Repetition, ? * +

Unlike range quantifiers, the repetition quantifiers (question mark “?”, asterisk “*”, and plus “+”) have few limits when performing regex searches, they are greedy. This is significant because these quantifiers settle for the minimum number of required matches, but always attempt to match as many times as possible, up to the maximum allowed. For example, the question mark “?” matches any preceding character 0 or 1 times, the asterisk “*” matches the preceding character 0 or more times, and the plus “+” matches the preceding character 1 or more times. Use repetition quantifiers in regex searches when large numbers of results are desired.

Using repetition to search for repeated characters with few limits.

Example 1: Use “?” to match the “u” character 0 or 1 times.

  • Regex:
    colou?r
  • Matches:
    colour
    color
  • Doesn't Match:
    colouur
    Colour

Example 2: Use “*” to match the preceding item 0 or more times; use “.” to match any character.

  • Regex:
    www\.my.*\.com
  • Matches:
    www.mysite.com
    www.mypage.com
    www.my.com
  • Doesn't Match:
    www.oursite.com
    mypage.com

Example 3: Use “+” to match the preceding “5” at least once.

  • Regex:
    bob5+@foo\.com
  • Matches:
    bob5@foo.com
    bob5555@foo.com
  • Doesn't Match:
    bob@foo.com
    bob65555@foo.com