Sunday, March 29, 2009

Regular Experessions - Part 2 - WildCards

To match repeated characters there are symbols for regular expression that makes the job possible.

Metacharacter Description Equivalent
------------- ----------- ----------
* Matches the preceding character or {0,}
subexpression zero or more times.

+ Matches the preceding character or {1,}
subexpression one or more times.

? Matches the preceding character or {0,1}
subexpression zero or one time.

{n} Matches the preceding character or
subexpression exactly n times. n>0

{n,m} Matches the preceding character or
subexpression at least n and at most
m times. (n > 0), (m > 0), (n <= m)

? (nongreedy) - A nongreedy pattern matches as
little of the searched string as
possible.
- A greedy pattern (default) matches
as much of the searched string as
possible.
? follows any other quantifiers [*,
+, ?, {n}, {n,m}], makes a nongreedy
pattern. Example: in string "aaaaa",
a+? matches a single 'a', but a+
matches all a's.

a|b Matches either a or b. a|book matches
a or book and (a|b)ook matches aook
or book.

[abcd] A character set, matches any one of
the enclosed characters. Example: [ab]
matches 'a' in "Plane".

[a-z] A range of characters. Example: to match
all alphanumeric characters [a-zA-Z0-9].

Share/Bookmark

No comments: