Iteration 'metacharacters'
The following is a set of iteration metacharacters (a.k.a. quantifiers) that can control the number of times a character or string is found in our searches.
? The ? (question mark) matches the preceding character 0 or 1 times only, for example, colou?r will find both color and colour.
* The * (asterisk or star) matches the preceding character 0 or more times, for example, tre* will find tree and tread and trough.
+ The + (plus) matches the previous character 1 or more times, for example, tre+ will find tree and tread but not trough.
{n} Matches the preceding character n times exactly, for example, to find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form 123-4567.
Note: The - (dash) in this case, because it is outside the square brackets, is a literal. Value is enclosed in braces (curly brackets).
{n,m} Matches the preceding character at least n times but not more than m times, for example, 'ba{2,3}b' will find 'baab' and 'baaab' but NOT 'bab' or 'baaaab'. Values are enclosed in braces (curly brackets).
So lets try them out with our example target strings.
Search for
\(.*l | STRING1 | match |
finds l in compatible (Note: The opening \ is an escape sequence used to indicate the ( it precedes is a literal not a metacharacter.)
|
STRING2 | no match |
Mozilla contains lls but not preceded by an open parenthesis (no match) and Linux has an upper case L (no match).
|
|
W*in | STRING1 | match |
Finds the Win in Windows.
|
STRING2 | match |
Finds in in Linux preceded by W zero times - so a match.
|
|
[xX][0-9a-z]{2} | STRING1 | no match |
Finds x in DigExt but only one t.
|
STRING2 | match |
Finds X and 11 in X11.
|
More 'metacharacters'
The following is a set of additional metacharacters that provide added power to our searches:
() The ( (open parenthesis) and ) (close parenthesis) may be used to group (or bind) parts of our search expression together
| The | (vertical bar or pipe) is called alternation in techspeak and means find the left hand OR right values, for example, gr(a|e)y will find 'gray' or 'grey'.
So lets try these out with our example strings..
Search for
^([L-Z]in) | STRING1 | match |
The '^' is an anchor indicating first position. Win does not start the string so no match.
|
STRING2 | no match |
The '^' is an anchor indicating first position. Linux does not start the string so no match.
|
|
((4\.[0-3])|(2\.[0-3])) | STRING1 | match |
Finds the 4.0 in Mozilla/4.0.
|
STRING2 | match |
Finds the 2.2 in Linux2.2.16-22.
|
|
(W|L)in | STRING1 | match |
Finds Win in Windows.
|
STRING2 | match |
Finds Lin in Linux.
|
More Stuff
For more information on regular expressions go to our links pages under Languages/regex. There are lots of folks who get a real buzz out of making any search a 'one liner' and they are incredibly helpful at telling you how they did it. Welcome to the wonderful, if arcane, world of Regular Expressions. You may want to play around with your new found knowledge using this tool.