Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Regular expression
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== The specific syntax rules vary depending on the specific implementation, [[programming language]], or [[library (computing)|library]] in use. Additionally, the functionality of regex implementations can vary between [[software versioning|versions]]. Because regexes can be difficult to both explain and understand without examples, interactive websites for testing regexes are a useful resource for learning regexes by experimentation. This section provides a basic description of some of the properties of regexes by way of illustration. The following conventions are used in the examples.<ref name="clarify000">The character 'm' is not always required to specify a [[Perl]] match operation. For example, <code>m/[^abc]/</code> could also be rendered as <code>/[^abc]/</code>. 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] {{Webarchive|url=https://web.archive.org/web/20091231010052/http://perldoc.perl.org/perlre.html |date=2009-12-31}}' for more details.</ref> 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 These regexes are all Perl-like syntax. Standard [[#POSIX Basic Regular Expressions|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, <code>\( \)</code> vs. <code>()</code>, or lack of <code>\d</code> instead of [[POSIX]] <code>[:digit:]</code>). The syntax and conventions used in these examples coincide with that of other programming environments as well.<ref>E.g., see ''[[Java (programming language)|Java]] [[O'Reilly Media#In a Nutshell|in a Nutshell]]'', p. 213; ''[[Python (programming language)|Python]] Scripting for Computational Science'', p. 320; Programming [[PHP]], p. 106.</ref> {| class="wikitable" |- ! Meta­character(s) ! Description ! Example<ref>All the if statements return a TRUE value</ref> |- ! <code>.</code> | Normally matches any character except a newline. <br/>Within square brackets the dot is literal. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/...../) { print "$string1 has length >= 5.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World has length >= 5. </syntaxhighlight> |- ! <code>( )</code> | Groups a series of pattern elements to a single element. <br/>When you match a pattern within parentheses, you can use any of <code>$1</code>, <code>$2</code>, ... later to refer to the previously matched pattern. Some implementations may use a backslash notation instead, like <code>\1</code>, <code>\2</code>. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/(H..).(o..)/) { print "We matched '$1' and '$2'.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> We matched 'Hel' and 'o W'. </syntaxhighlight> |- ! <code>+</code> | Matches the preceding pattern element one or more times. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/l+/) { print "There are one or more consecutive letter \"l\"'s in $string1.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> There are one or more consecutive letter "l"'s in Hello World. </syntaxhighlight> |- ! <code>?</code> | Matches the preceding pattern element zero or one time. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/H.?e/) { print "There is an 'H' and a 'e' separated by "; print "0-1 characters (e.g., He Hue Hee).\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> There is an 'H' and a 'e' separated by 0-1 characters (e.g., He Hue Hee). </syntaxhighlight> |- ! <code>?</code> | Modifies the <code>*</code>, <code>+</code>, <code>?</code> or <code>{M,N}</code>'d regex that comes before to match as few times as possible. | <syntaxhighlight lang="perl"> $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"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> The non-greedy match with 'l' followed by one or more characters is 'llo' rather than 'llo Wo'. </syntaxhighlight> |- ! <code>*</code> | Matches the preceding pattern element zero or more times. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/el*o/) { print "There is an 'e' followed by zero to many "; print "'l' followed by 'o' (e.g., eo, elo, ello, elllo).\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> There is an 'e' followed by zero to many 'l' followed by 'o' (e.g., eo, elo, ello, elllo). </syntaxhighlight> |- ! <code>{M,N}</code> | Denotes the minimum M and the maximum N match count.<br/>N can be omitted and M can be 0: <code>{M}</code> matches "exactly" M times; <code>{M,}</code> matches "at least" M times; <code>{0,N}</code> matches "at most" N times.<br/><code>x* y+ z?</code> is thus equivalent to <code>x{0,} y{1,} z{0,1}</code>. | <syntaxhighlight lang="perl"> $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"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> There exists a substring with at least 1 and at most 2 l's in Hello World </syntaxhighlight> |- ! <code>[β¦]</code> | Denotes a set of possible character matches. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/[aeiou]+/) { print "$string1 contains one or more vowels.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World contains one or more vowels. </syntaxhighlight> |- ! <code><nowiki>|</nowiki></code> | Separates alternate possibilities. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/(Hello|Hi|Pogo)/) { print "$string1 contains at least one of Hello, Hi, or Pogo."; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World contains at least one of Hello, Hi, or Pogo. </syntaxhighlight> |- ! <code>\b</code> | Matches a zero-width boundary between a word-class character (see next) and either a non-word class character or an edge; same as <code>(^\w|\w$|\W\w|\w\W)</code>. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/llo\b/) { print "There is a word that ends with 'llo'.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> There is a word that ends with 'llo'. </syntaxhighlight> |- ! <code>\w</code> | Matches an alphanumeric character, including "_"; <br/>same as <code>[A-Za-z0-9_]</code> in ASCII, and : <code>[\p{Alphabetic}<wbr/>\p{GC=Mark}<wbr/>\p{GC=Decimal_Number}<wbr/>\p{GC=Connector_Punctuation}]</code> in Unicode,<ref name="unicode"/> where the <code>Alphabetic</code> property contains more than Latin letters, and the <code>Decimal_Number</code> property contains more than Arab digits. | <syntaxhighlight lang="perl"> $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"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> There is at least one alphanumeric character in Hello World (A-Z, a-z, 0-9, _). </syntaxhighlight> |- ! <code>\W</code> | Matches a ''non''-alphanumeric character, excluding "_"; <br/>same as <code>[^A-Za-z0-9_]</code> in ASCII, and : <code>[^\p{Alphabetic}<wbr/>\p{GC=Mark}<wbr/>\p{GC=Decimal_Number}<wbr/>\p{GC=Connector_Punctuation}]</code> in Unicode. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/\W/) { print "The space between Hello and "; print "World is not alphanumeric.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> The space between Hello and World is not alphanumeric. </syntaxhighlight> |- ! <code>\s</code> | Matches a whitespace character, <br/>which in ASCII are tab, line feed, form feed, carriage return, and space; <br/>in Unicode, also matches no-<wbr/>break spaces, next line, and the variable-<wbr/>width spaces (among others). | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/\s.*\s/) { print "In $string1 there are TWO whitespace characters, which may"; print " be separated by other characters.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> In Hello World there are TWO whitespace characters, which may be separated by other characters. </syntaxhighlight> |- ! <code>\S</code> | Matches anything ''but'' a whitespace. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/\S.*\S/) { print "In $string1 there are TWO non-whitespace characters, which"; print " may be separated by other characters.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> In Hello World there are TWO non-whitespace characters, which may be separated by other characters. </syntaxhighlight> |- ! <code>\d</code> | Matches a digit; <br/>same as <code>[0-9]</code> in ASCII; <br/>in Unicode, same as the <code>\p{Digit}</code> or <code>\p{GC=Decimal_Number}</code> property, which itself the same as the <code>\p{Numeric_Type=Decimal}</code> property. | <syntaxhighlight lang="perl"> $string1 = "99 bottles of beer on the wall."; if ($string1 =~ m/(\d+)/) { print "$1 is the first number in '$string1'\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> 99 is the first number in '99 bottles of beer on the wall.' </syntaxhighlight> |- ! <code>\D</code> | Matches a non-digit; <br/>same as <code>[^0-9]</code> in ASCII or <code>\P{Digit}</code> in Unicode. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/\D/) { print "At least one character in $string1"; print " is not a digit.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> At least one character in Hello World is not a digit. </syntaxhighlight> |- ! <code>^</code> | Matches the beginning of a line or string. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/^He/) { print "$string1 starts with the characters 'He'.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World starts with the characters 'He'. </syntaxhighlight> |- ! <code>$</code> | Matches the end of a line or string. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/rld$/) { print "$string1 is a line or string "; print "that ends with 'rld'.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World is a line or string that ends with 'rld'. </syntaxhighlight> |- ! <code>\A</code> | Matches the beginning of a string (but not an internal line). | <syntaxhighlight lang="perl"> $string1 = "Hello\nWorld\n"; if ($string1 =~ m/\AH/) { print "$string1 is a string "; print "that starts with 'H'.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World is a string that starts with 'H'. </syntaxhighlight> |- ! <code>\z</code> | Matches the end of a string (but not an internal line).<ref name="Perl Best Practices">{{cite book |last=Conway |first=Damian |author-link=Damian Conway |title=Perl Best Practices |chapter=Regular Expressions, End of String |publisher=[[O'Reilly Media|O'Reilly]] |page=240 |chapter-url=https://www.scribd.com/doc/15491004/Perl-Best-Practices |year=2005 |isbn=978-0-596-00173-5 |access-date=2017-09-10 |archive-date=2020-10-07 |archive-url=https://web.archive.org/web/20201007183212/https://www.scribd.com/book/15491004/Perl-Best-Practices-Standards-and-Styles-for-Developing-Maintainable-Code |url-status=live}}</ref> | <syntaxhighlight lang="perl"> $string1 = "Hello\nWorld\n"; if ($string1 =~ m/d\n\z/) { print "$string1 is a string "; print "that ends with 'd\\n'.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World is a string that ends with 'd\n'. </syntaxhighlight> |- ! <code>[^β¦]</code> | Matches every character except the ones inside brackets. | <syntaxhighlight lang="perl"> $string1 = "Hello World\n"; if ($string1 =~ m/[^abc]/) { print "$string1 contains a character other than "; print "a, b, and c.\n"; } </syntaxhighlight> '''Output:''' <syntaxhighlight lang="output"> Hello World contains a character other than a, b, and c. </syntaxhighlight> |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)