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!
==Basic concepts== A regular expression, often called a ''pattern'', specifies a [[set (computer science)|set]] of strings required for a particular purpose. A simple way to specify a finite set of strings is to list its [[data element|elements]] or members. However, there are often more concise ways: for example, the set containing the three strings "Handel", "Händel", and "Haendel" can be specified by the pattern <code>H(ä|ae?)ndel</code>; we say that this pattern ''matches'' each of the three strings. However, there can be many ways to write a regular expression for the same set of strings: for example, <code>(Hän|Han|Haen)del</code> also specifies the same set of three strings in this example. Most formalisms provide the following operations to construct regular expressions. ; Boolean "or" : : A [[vertical bar]] separates alternatives. For example, {{code|lang=perl|code=gray{{!}}grey}} can match "gray" or "grey". ; Grouping : [[Parentheses]] are used to define the scope and precedence of the [[operator (programming)|operators]] (among other uses). For example, <code>gray|grey</code> and {{code|lang=perl|code=gr(a{{!}}e)y}} are equivalent patterns which both describe the set of "gray" or "grey". ; Quantification : A [[quantifier (linguistics)|quantifier]] after an element (such as a [[lexical analysis#Token|token]], character, or group) specifies how many times the preceding element is allowed to repeat. The most common quantifiers are the [[question mark]] <code>?</code>, the [[asterisk]] <code>*</code> (derived from the [[Kleene star]]), and the [[plus sign]] <code>+</code> ([[Kleene plus]]). : {| |- | style="width:15px; vertical-align:top;" |<code>'''?'''</code> |The question mark indicates ''zero or one'' occurrences of the preceding element. For example, <code><!--DON'T CHANGE THIS TO "colo?r"; REGULAR EXPRESSIONS DON'T WORK LIKE WILDCARDS!-->colou?r</code> matches both "color" and "colour". |- | style="vertical-align:top;" |<code>'''<nowiki>*</nowiki>'''</code> |The asterisk indicates ''zero or more'' occurrences of the preceding element. For example, <code>ab*c</code> matches "ac", "abc", "abbc", "abbbc", and so on. |- | style="vertical-align:top;" |<code>'''+'''</code> |The plus sign indicates ''one or more'' occurrences of the preceding element. For example, <code>ab+c</code> matches "abc", "abbc", "abbbc", and so on, but not "ac". |- |<code>'''{n}'''</code><ref name="grep">{{cite web |last=Kerrisk |first=Michael |title=grep(1) - Linux manual page |url=https://man7.org/linux/man-pages/man1/grep.1.html |website=man7.org |access-date=31 January 2023}}</ref> | The preceding item is matched exactly ''n'' times. |- |<code>'''{min,}'''</code><ref name="grep"/> | The preceding item is matched ''min'' or more times. |- |<code>'''{,max}'''</code><ref name="grep"/> | The preceding item is matched up to ''max'' times. |- |<code>'''{min,max}'''</code><ref name="grep"/> | The preceding item is matched at least ''min'' times, but not more than ''max'' times. |} ; Wildcard : : The wildcard <code>'''.'''</code> matches any character. For example, :: <code>a.b</code> matches any string that contains an "a", and then any character and then "b". :: <code>a.*b</code> matches any string that contains an "a", and then the character "b" at some later point. These constructions can be combined to form arbitrarily complex expressions, much like one can construct arithmetical expressions from numbers and the operations +, −, ×, and ÷. The precise [[syntax]] for regular expressions varies among tools and with context; more detail is given in {{section link||Syntax}}.
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)