Regular Expression - Examples

Examples

This article may need to be rewritten entirely to comply with Wikipedia's quality standards. You can help. The discussion page may contain suggestions.
This article may contain excessive, poor, or irrelevant examples. Please improve the article by adding more descriptive text and removing less pertinent examples. See Wikipedia's guide to writing better articles for further suggestions.

A regular expression is a string that is used to describe or match a set of strings according to certain syntax rules. The specific syntax rules vary depending on the specific implementation, programming language, or library in use. Additionally, the functionality of regex implementations can vary between versions.

Despite this variability, and because regular expressions can be difficult to both explain and understand without examples, this article provides a basic description of some of the properties of regular expressions by way of illustration.

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.

Unless otherwise indicated, the following examples conform to the Perl programming language, release 5.8.8, January 31, 2006. This means that other implementations may lack support for some parts of the syntax shown here (e.g. basic vs. extended regex, \( \) vs., or lack of \d instead of POSIX ).

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"; if ($string1 =~ m/...../) { print "$string1 has length >= 5\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"; if ($string1 =~ m/(H..).(o..)/) { print "We matched '$1' and '$2'\n"; } Output: We matched 'Hel' and 'o W';
+ Matches the preceding pattern element one or more times. $string1 = "Hello World\n"; if ($string1 =~ m/l+/) { print "There are one or more consecutive letter \"l\"'s in $string1\n"; } Output: There are one or more consecutive letter "l"'s in Hello World
? Matches the preceding pattern element zero or one times. $string1 = "Hello World\n"; if ($string1 =~ m/H.?e/) { print "There is an 'H' and a 'e' separated by "; print "0-1 characters (Ex: He Hoe)\n"; }
? Modifies the *, +, or {M,N}'d regex that comes before to match as few times as possible. $string1 = "Hello World\n"; if ($string1 =~ m/(l.+?o)/) { print "The non-greedy match with 'l' followed by one or "; print "more characters is 'llo' rather than 'llo wo'.\n"; }
* Matches the preceding pattern element zero or more times. $string1 = "Hello World\n"; if ($string1 =~ m/el*o/) { print "There is an 'e' followed by zero to many "; print "'l' followed by 'o' (eo, elo, ello, elllo)\n"; }
{M,N} Denotes the minimum M and the maximum N match count. $string1 = "Hello World\n"; if ($string1 =~ m/l{1,2}/) { print "There exists a substring with at least 1 "; print "and at most 2 l's in $string1\n"; }
Denotes a set of possible character matches. $string1 = "Hello World\n"; if ($string1 =~ m/+/) { print "$string1 contains one or more vowels.\n"; }
| Separates alternate possibilities. $string1 = "Hello World\n"; if ($string1 =~ m/(Hello|Hi|Pogo)/) { print "At least one of Hello, Hi, or Pogo is "; print "contained in $string1.\n"; }
\b Matches a zero-width boundary between a word-class character (see next) and either a non-word class character or an edge. $string1 = "Hello World\n"; if ($string1 =~ m/llo\b/) { print "There is a word that ends with 'llo'\n"; }
\w Matches an alphanumeric character, including "_"; same as in ASCII. In Unicode same as, where the Alphabetic property contains more than just Letters, and the Decimal_Number property contains more than . $string1 = "Hello World\n"; if ($string1 =~ m/\w/) { print "There is at least one alphanumeric "; print "character in $string1 (A-Z, a-z, 0-9, _)\n"; }
\W Matches a non-alphanumeric character, excluding "_"; same as in ASCII, and in Unicode. $string1 = "Hello World\n"; if ($string1 =~ m/\W/) { print "The space between Hello and "; print "World is not alphanumeric\n"; }
\s Matches a whitespace character, which in ASCII are tab, line feed, form feed, carriage return, and space; in Unicode, also matches no-break spaces, next line, and the variable-width spaces (amongst others). $string1 = "Hello World\n"; if ($string1 =~ m/\s.*\s/) { print "There are TWO whitespace characters, which may"; print " be separated by other characters, in $string1"; }
\S Matches anything BUT a whitespace. $string1 = "Hello World\n"; if ($string1 =~ m/\S.*\S/) { print "There are TWO non-whitespace characters, which"; print " may be separated by other characters, in $string1"; }
\d Matches a digit; same as in ASCII; in Unicode, same as the \p{Digit} or \p{GC=Decimal_Number} property, which itself the same as the \p{Numeric_Type=Decimal} property. $string1 = "99 bottles of beer on the wall."; if ($string1 =~ m/(\d+)/) { print "$1 is the first number in '$string1'\n"; } Output: 99 is the first number in '99 bottles of beer on the wall.'
\D Matches a non-digit; same as in ASCII or \P{Digit} in Unicode. $string1 = "Hello World\n"; if ($string1 =~ m/\D/) { print "There is at least one character in $string1"; print " that is not a digit.\n"; }
^ Matches the beginning of a line or string. $string1 = "Hello World\n"; if ($string1 =~ m/^He/) { print "$string1 starts with the characters 'He'\n"; }
$ Matches the end of a line or string. $string1 = "Hello World\n"; if ($string1 =~ m/rld$/) { print "$string1 is a line or string "; print "that ends with 'rld'\n"; }
\A Matches the beginning of a string (but not an internal line). $string1 = "Hello\nWorld\n"; if ($string1 =~ m/\AH/) { print "$string1 is a string "; print "that starts with 'H'\n"; }
\z Matches the end of a string (but not an internal line).
see Perl Best Practices — Page 240
$string1 = "Hello\nWorld\n"; if ($string1 =~ m/d\n\z/) { print "$string1 is a string "; print "that ends with 'd\\n'\n"; }
Matches every character except the ones inside brackets. $string1 = "Hello World\n"; if ($string1 =~ m//) { print "$string1 contains a character other than "; print "a, b, and c\n"; }

Read more about this topic:  Regular Expression

Famous quotes containing the word examples:

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)