A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.
Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.
The .* is a wildcard expression that matches any sequence of characters including an empty sequence of length=0. grep a.*z matches all of the following strings that start with a and end with z: "abcdefghijklmnopqrstuvwxyz", "abz", "abbz", "ahhhhhz" and "abbdz".
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
See also a lot of general hints and useful links at the regex tag details page.
Online tutorials
Quantifiers
*:greedy, *?:reluctant, *+:possessive
+:greedy, +?:reluctant, ++:possessive
?:optional (zero-or-one){n,m}:between n & m, {n,}:n-or-more, {n}:exactly n
{n} and {n}?Character Classes
[...]: any one character, [^...]: negated/any character but[^] matches any one character including newlines javascript
[\w-[\d]] / [a-z-[qz]]: set subtraction .net, xml-schema, xpath, JGSoft[\w&&[^\d]]: set intersection java, ruby 1.9+[[:alpha:]]:POSIX character classes[[:<:]] and [[:>:]] Word boundaries[^\\D2], [^[^0-9]2], [^2[^0-9]] get different results in Java? java
\d:digit, \D:non-digit
\w:word character, \W:non-word character
\s:whitespace, \S:non-whitespace
\p{L}, \P{L}, etc.)Escape Sequences
\h:space-or-tab, \t:tab
\r, \n:carriage return and line feed\R:generic newline php java-8
\H:Non horizontal whitespace character, \V:Non vertical whitespace character, \N:Non line feed character pcre php5 java-8
\v:vertical tab, \e:the escape character
Anchors
^:start of line/input, \b:word boundary, and \B:non-word boundary, $:end of line/input
\A:start of input, \Z:end of input php, perl, ruby
\z:the very end of input (\Z in Python) .net, php, pcre, java, ruby, icu, swift, objective-c
\G:start of match php, perl, ruby
(Also see "Flavor-Specific Information → Java → The functions in Matcher")
Groups
(...):capture group, (?:):non-capture group
\1:backreference and capture-group reference, $1:capture group reference
\g<1>123:How to follow a numbered capture group, such as \1, with a number?: python
(?i:regex) mean?(?P<group_name>regexp) mean?(?>):atomic group or independent group, (?|):branch reset
regular-expressions.info(?<groupname>regex): Overview and naming rules (Non-Stack Overflow links)
(?P<groupname>regex) python, (?<groupname>regex) .net, (?<groupname>regex) perl, (?P<groupname>regex) and (?<groupname>regex) php
Lookarounds
(?=...):positive, (?!...):negative
(?<=...):positive, (?<!...):negative
{0,n} java
\K php, perl (Flavors that support \K)Modifiers
| flag | modifier | flavors |
|---|---|---|
a |
ASCII | python |
c |
current position | perl |
e |
expression | php perl |
g |
global | most |
i |
case-insensitive | most |
m |
multiline | php perl python javascript .net java |
m |
(non)multiline | ruby |
o |
once | perl ruby |
S |
study | php |
s |
single line | ruby |
U |
ungreedy | php r |
u |
unicode | most |
x |
whitespace-extended | most |
y |
sticky ↪ | javascript |
Other:
|:alternation (OR) operator, .:any character, [.]:literal dot character
(*PRUNE), (*SKIP), (*FAIL) and (*F)
(*BSR_ANYCRLF)
(?R), (?0) and (?1), (?-1), (?&groupname)
Common Tasks
{...}Advanced Regex-Fu
(?!a)athis except in contexts A, B and CFlavor-Specific Information
(Except for those marked with *, this section contains non-Stack Overflow links.)
java.util.regex.Matcher:
matches()): The match must be anchored to both input-start and -endfind()): A match may be anywhere in the input string (substrings)lookingAt(): The match must be anchored to input-start onlyjava.lang.String functions that accept regular expressions: matches(s), replaceAll(s,s), replaceFirst(s,s), split(s), split(s,i)
java.util.regex
MySQL
Oracle
Perl5 version 18.2
preg_match
search vs match, how-to
regex, struct regex::Regex
regexp command
General information
(Links marked with * are non-Stack Overflow links.)
Please, just don't
Examples of regex that can cause regex engine to fail
Tools: Testers and Explainers
(This section contains non-Stack Overflow links.)
Online (* includes replacement tester, + includes split tester):
RegExr
Regex Hero dotnet
freeformatter.com xregexp
regex.larsolavtorvik.com php PCRE and POSIX, javascript
Offline:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With