Character match

Matches the exact characters in the sequence1:

Beans (capital B) does not match:

Matches beans with either B and b in the beginning:


Excludes all bean:

  • s?: Matches none or one s

  • \w+: Any character except spaces. Can be one or more character.
  • \w+\s: Like above but must have one space at the end
  • Matches any word with [Bb]eans as the second word
  • Beans does not match in this example because previous characters are comma and space

Range match

  • [a-z]: Any one character from small a to z
  • [a-z]+: One or more characters from small a to z
  • [a-z]+e: Like the above but must have an e after it
  • [a-z]+e\b: Also like the above but must have no more characters after it (boundary)

Meta-characters

  • .: Any character that isn’t \n
  • \s: The whitespace character
  • \d: Any number digit (i.e. [0-9])
  • \D: Not \d
  • \w: Any alphabets, numbers, and _
  • \W: Not \w (i.e. punctuations, space, etc.)

Examples

  • .....: Matches any 5 characters that isn’t \n
  • .{5}: Same as above

  • \w+\d+@: Matches one or more characters with one or more numbers before the @ (i.e. [a-z]+[0-9]+@)
  • [a-z]+[^0-9\s]+@: Matches any email address that does not contain numbers and/or whitespaces

Anchors

  • ^ or \A: At the beginning of the line

  • $ or \Z: At the end of the line

  • \b: Word boundary
  • \B: Non-word boundary (i.e. in-between the characters of a word)

Quantifiers

  • *: Zero or more
  • +: One or more
  • ?: Zero or one
  • {m,n}: Between m and n inclusive

Remember, only the letter i is matched here.


  1. Beans has capital B and bean has no s that’s why they’re not a match [return]