Regular expression examples

Regular expression examples

A regular expression ( also "RegEx" or "regex" ) 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.

Conventions

The following conventions are used in the examples.The character 'm' is not always required to specify a perl match operation. For example, m/ [^abc] / could also be rendered as / [^abc] /. The 'm' is only necessary if the user wishes to specify a match operation without using a forward-slash as the regex delimiter. Sometimes it is useful to specify an alternate regex delimiter in order to avoid "delimiter collision". See ' [http://perldoc.perl.org/perlre.html perldoc perlre] ' for more details.]

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

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)DescriptionExample
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 ";if ($string1 =~ m/...../) { print "$string1 has length >= 5 ";}
( )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 ";if ($string1 =~ m/(H..).(o..)/) { print "We matched '$1' and '$2' ";}

Output:
We matched 'Hel' and 'o W';
+Matches the preceding pattern element one or more times.
$string1 = "Hello World ";if ($string1 =~ m/l+/) { print "There are one or more consecutive letter "l"'s in $string1 ";}
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 ";if ($string1 =~ m/H.?e/) { print "There is an 'H' and a 'e' separated by "; print "0-1 characters (Ex: He Hoe) ";}
?Modifies the *, +, or {M,N}'d regexp that comes beforeto match as few times as possible.
$string1 = "Hello World ";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'. ";}
*Matches the preceding pattern element zero or more times.
$string1 = "Hello World ";if ($string1 =~ m/el*o/) { print "There is an 'e' followed by zero to many "; print "'l' followed by 'o' (eo, elo, ello, elllo) ";}
{M,N}Denotes the minimum M and the maximum N match count.
$string1 = "Hello World ";if ($string1 =~ m/l{1,2}/) { print "There exists a substring with at least 1 "; print "and at most 2 l's in $string1 ";}
[...] Denotes a set of possible character matches.
$string1 = "Hello World ";if ($string1 =~ m/ [aeiou] +/) { print "$string1 contains one or more vowels. ";}
|Separates alternate possibilities.
$string1 = "Hello World ";if ($string1 =~ m/(Hello|Hi|Pogo)/) { print "At least one of Hello, Hi, or Pogo is "; print "contained in $string1. ";}
Matches a word boundary.
$string1 = "Hello World ";if ($string1 =~ m/llo/) { print "There is a word that ends with 'llo' ";} else { print "There are no words that end with 'llo' ";}
wMatches an alphanumeric character, including "_".
$string1 = "Hello World ";if ($string1 =~ m/w/) { print "There is at least one alphanumeric "; print "character in $string1 (A-Z, a-z, 0-9, _) ";}
WMatches a non-alphanumeric character, excluding "_".
$string1 = "Hello World ";if ($string1 =~ m/W/) { print "The space between Hello and "; print "World is not alphanumeric ";}
sMatches a whitespace character (space, tab, newline, form feed)
$string1 = "Hello World ";if ($string1 =~ m/s.*s/) { print "There are TWO whitespace characters, which may"; print " be separated by other characters, in $string1";}
SMatches anything BUT a whitespace.
$string1 = "Hello World ";if ($string1 =~ m/S.*S/) { print "There are TWO non-whitespace characters, which"; print " may be separated by other characters, in $string1";}
dMatches a digit, same as [0-9] .
$string1 = "99 bottles of beer on the wall.";if ($string1 =~ m/(d+)/) { print "$1 is the first number in '$string1' ";}

Output:
99 is the first number in '99 bottles of beer on the wall.'
DMatches a non-digit.
$string1 = "Hello World ";if ($string1 =~ m/D/) { print "There is at least one character in $string1"; print " that is not a digit. ";}
^Matches the beginning of a line or string.
$string1 = "Hello World ";if ($string1 =~ m/^He/) { print "$string1 starts with the characters 'He' ";}
$Matches the end of a line or string.
$string1 = "Hello World ";if ($string1 =~ m/rld$/) { print "$string1 is a line or string "; print "that ends with 'rld' ";}
AMatches the beginning of a string (but not an internal line).
$string1 = "Hello World ";if ($string1 =~ m/AH/) { print "$string1 is a string "; print "that starts with 'H' ";}
Matches the end of a string (but not an internal line).
$string1 = "Hello World ";if ($string1 =~ m/d /) { print "$string1 is a string "; print "that ends with 'd\n' ";}
[^...] Matches every character except the ones inside brackets.
$string1 = "Hello World ";if ($string1 =~ m/ [^abc] /) { print "$string1 contains a character other than "; print "a, b, and c ";}

Notes

See also

* Comparison of programming languages


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Regular expression — In computing, a regular expression provides a concise and flexible means for matching (specifying and recognizing) strings of text, such as particular characters, words, or patterns of characters. Abbreviations for regular expression include… …   Wikipedia

  • Regular language — In theoretical computer science, a regular language is a formal language (i.e., a possibly infinite set of finite sequences of symbols from a finite alphabet) that satisfies the following equivalent properties: * it can be accepted by a… …   Wikipedia

  • Parsing expression grammar — A parsing expression grammar, or PEG, is a type of analytic formal grammar that describes a formal language in terms of a set of rules for recognizing strings in the language. A parsing expression grammar essentially represents a recursive… …   Wikipedia

  • Facial expression — Photographs from the 1862 book Mécanisme de la Physionomie Humaine by Guillaume Duchenne. Through electric stimulation, Duchenne determined which muscles were responsible for different facial expressions. Charles Darwin would later republish some …   Wikipedia

  • M-expression — In computer programming, M expressions (or meta expressions) were intended to be the expressions used to write functions in the Lisp programming language. Data to be manipulated using M expressions was to be written using S expressions. M… …   Wikipedia

  • Delimiter — This article is about delimiters in computing. For delimiters in written human languages, see interword separation. A stylistic depiction of a fragment from a CSV formatted text file. The commas (shown in red) are used as field delimiters. A… …   Wikipedia

  • Perl — Desarrollador(es) Larry Wall www.perl.org Información general Paradigma multiparadigma, funcional, im …   Wikipedia Español

  • Scunthorpe Problem — The Scunthorpe Problem occurs when a spam filter or search engine blocks e mails or search results because their text contains a string of letters that are shared by an obscene word. While computers can easily identify strings of text within a… …   Wikipedia

  • C++11 — C++11, also formerly known as C++0x,[1] is the name of the most recent iteration of the C++ programming language, replacing C++TR1, approved by the ISO as of 12 August 2011.[2] The name is derived from the tradition of naming language versions by …   Wikipedia

  • JavaScript syntax — This article is part of the JavaScript series. JavaScript JavaScript syntax JavaScript topics This box: view · …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”