2015-07-22 16:44:07 -04:00
..
2015-07-22 16:44:07 -04:00

Regular Expressions

Regular Expressions (regex for short) are a way to represent patterns in strings. They aren't as bad as people make them out to be. When used on the right problem, they are a great solution.

Go to Rubular. We're going to sandbox inside it a bit to get acquainted with regex syntax. At the bottom of the page is a regex cheat sheet.
Note: Depending on the language, you may have to escape certain characters. For example, instead of a{3}, you may have to type a\{3\}.
Also, be careful with periods (.). A period means any character in regex, so you have to escape it if you want to pattern match a period.

Here are some examples of regex and matching string patterns:

  • a{3} => aaa
  • a{3,} => aaaaaaaa
  • [a-z] => any lowercase letter
  • [A-Z]{4,6} => between 4 and 6 uppercase letters, ex. FASDV
  • . => any character
  • \w\. => _.
  • hello\d? => hello or hello5
  • [a-nO-Z]* => abcOZZ

Make a regex to match each of these strings: