Recall: Regex Syntax Reference

| or
() grouping
^ start
$ end
special chars
* 0 or more
+ 1 or more
? 0 or 1
{min,max} between min and max
quantifiers
[abcde] one of those characters
[a-z] a character from a through z
\d digit
\s whitespace
character sets

Exercise : Regular Expressions (by Zack Cava)

Write a regular expression (slides) that would match the following kinds of patterns. You can use the site Rubular to test your regex.

  1. a student's letter grade such as A, B+, or D- (try it) (data)
  2. a DNA sequence (string of any combination of A, C, G, T) (try it) (data)
  3. a US ZIP code (try it) (data)
  4. a credit card number, with optional dashes every 4 numbers (try it) (data)
  5. a real number such as 3.14 or -42.8775 (try it) (data)

Regular Expressions, continued

  1. a dollar amount of at least $100.00 (try it) (data)
  2. a word with two or more vowels (A, E, I, O, or U) (try it) (data)
  3. a string that starts with either 'q' or 's', and that also contains a double z ('zz') later within the same word (try it) (data)
  4. a string that contains at least 5 vowels (a, e, i, o, or u) in a row (try it) (data)
  5. a string that is at least 25 characters long (try it) (data)

Regular Expressions, continued

  1. a string that ends with two or more occurrences of "?!", as in, "Huh?!?!" (try it) (data)
  2. a valid amount for a US coin: 1, 5, 10, 25, or 50 cents (try it) (data)
  3. an IP address, of the format 128.208.3.88 (accept any 3-digit numbers between the dots) (try it) (data)
  4. a string that contains at least 3 words, each separated by one or more spaces (try it) (data)
  5. a string that contains a quotation with "quote marks" around it (try it) (data)

Exercise Solutions

  1. letter grade: /^[ABCDF][+\-]?$/
    • if F+/F- are not allowed: /^[ABCDF][+\-]|F$/
  2. DNA sequence: /^[ACGT]+$/
  3. US ZIP: /^\d{5}(-\d{4})?$/ (with optional “+4”)
  4. credit card with optional dashes: /^\d{4}(-?\d{4}){3}$/
  5. real number: /^[-]?\d+(\.\d+)?$/
    • to allow for no leading zero (e.g., .16667): /^[-]?(\d+(\.\d+)?|\.\d+)$/
    • why not just /^[-]?\d*(\.\d+)?$/?

Exercise Solutions, continued

  1. $100+: /^\$[1-9]\d{2,}\.\d{2}$/
  2. word with 2 or more vowels: /^[a-z]*[aeiou][a-z]*[aeiou][a-z]*$/i
    • or /^[a-z]*([aeiou][a-z]*[aeiou][a-z]*){2,}$/i
  3. string starting with q or s and zz within same word: /^[qs][^ ]*zz/ (not /^[qs].*zz/)
  4. 5 or more vowels in a row: /[aeiou]{5}/i (why don’t we need {5,}?)
  5. 25 or more characters long: /.{25}/ (why don’t we need {25,}?)

Exercise Solutions, continued

  1. string ending with 2 or more '?!': /(\?!){2}$/ (why don’t we need {2,}?)
  2. US coin amount: /^1|5|10|25|50$/ or /^10?|50?|25$/
  3. IP address: /^\d{1,3}(\.\d{1,3}){3}$/
  4. string with 3+ words: /[a-z]+( +[a-z]+){2}/i (why don’t we need {2,}?)
  5. string containing a quote: /"[^"]+"/