Conventions
The following conventions are used in the examples.
metacharacter(s) ;; the metacharacters column specifies the regex syntax being demonstrated
=~ m// ;; indicates a regex match operation in perl
=~ s/// ;; indicates a regex substitution operation in perl
Also worth noting is that these regular expressions are all Perl-like syntax. Standard POSIX regular expressions are different.
[edit] Examples
Unless otherwise indicated, the following examples conform to the Perl programming language, release 5.8.8, January 31, 2006. The syntax and conventions used in these examples coincide with that of other programming environments as well (e.g., see Java in a Nutshell - Page 213, Python Scripting for Computational Science - Page 320, Programming PHP - Page 106 ).
Metacharacter(s) | Description | Example Note that all the if statements return a TRUE value |
---|---|---|
. | Normally matches any character except a newline. Within square brackets the dot is literal. | $string1 = "Hello World\n"; |
( ) | Groups a series of pattern elements to a single element. When you match a pattern within parentheses, you can use any of $1, $2, ... later to refer to the previously matched pattern. | $string1 = "Hello World\n";Output:
|
+ | Matches the preceding pattern element one or more times. | $string1 = "Hello World\n";Output:
|
? | Matches the preceding pattern element zero or one times. | $string1 = "Hello World\n"; |
? | Modifies the *, +, or {M,N}'d regexp that comes before to match as few times as possible. | $string1 = "Hello World\n"; |
* | Matches the preceding pattern element zero or more times. | $string1 = "Hello World\n"; |
{M,N} | Denotes the minimum M and the maximum N match count. | $string1 = "Hello World\n"; |
[...] | Denotes a set of possible character matches. | $string1 = "Hello World\n"; |
| | Separates alternate possibilities. | $string1 = "Hello World\n"; |
\b | Matches a word boundary. | $string1 = "Hello World\n"; |
\w | Matches an alphanumeric character, including "_". | $string1 = "Hello World\n"; |
\W | Matches a non-alphanumeric character, excluding "_". | $string1 = "Hello World\n"; |
\s | Matches a whitespace character (space, tab, newline, form feed) | $string1 = "Hello World\n"; |
\S | Matches anything BUT a whitespace. | $string1 = "Hello World\n"; |
\d | Matches a digit, same as [0-9]. | $string1 = "99 bottles of beer on the wall.";Output:
|
\D | Matches a non-digit. | $string1 = "Hello World\n"; |
^ | Matches the beginning of a line or string. | $string1 = "Hello World\n"; |
$ | Matches the end of a line or string. | $string1 = "Hello World\n"; |
\A | Matches the beginning of a string (but not an internal line). | $string1 = "Hello\nWorld\n"; |
\Z | Matches the end of a string (but not an internal line). | $string1 = "Hello\nWorld\n"; |
[^...] | Matches every character except the ones inside brackets. | $string1 = "Hello World\n"; |
http://en.wikipedia.org/wiki/Regular_expression_examples
1 comment:
Perl is the best scripting language for Text processing and handle regex. I have posted few articles related to those at my blog
http://icfun.blogspot.com/search/label/perl
Also Perl's Cpan has lots of support that I don't even need to think extra while developing project. I didn't find such help on other programming language except Java and .NET
Post a Comment