Everything you have to know about Regex


.Any single character; wildcard; joker
^Start of string
$End of string
\nNewline
\rCarriage return
\tTab
\0Null character
// Quantifiers
a?Zero or one of a// siehe lazy-quatifier
a*Zero or more of a
a+One or more of a// greedy quatifier --> siehe lazy-quantifier
a+?// siehe lazy-quatifier
a{3}Exactly 3 of a
a{3,}3 or more of a!
a{3,6}Between 3 and 6 of a
a*Greedy quantifier
a*?Lazy quantifier
a*+Possessive quantifier
// Group construcs
(...)Capture everything enclosed!!
(a|b)Match either a or b// I love (cats|dogs)
(?:...)Match everything enclosed!
(?>...)Atomic group [possessive]
(?|...)Duplicate subpattern group!
(?#...)Comment
(?'name'...)Named capturing group
(?...)Named capturing group
(?P...)Named capturing group
(?imsxXU)Inline modifiers
(?(...)|)Conditional statement
(?R)Recurse entire pattern
(?1)Recurse first subpattern
(?+1)Recurse first relative subpattern
(?&name)Recurse subpattern `name`
(?P>name)Recurse subpattern `name`
(?=...)Positive lookahead// wie ein Anker / anchor!!
// Monday\s(?=Tuesday)
// --> Monday which is followed by a Tuesday
(?!...)Negative lookahead// Negative --> Verneinung
(?<=...)Positive lookbehind
(?Negative lookbehind
(*UTF16)Verbs
(a(bc))Capture subgroup
// Character classes
[abc]A single character of: a, b or c
[^abc]A character except: a, b or c
[a-z]A character in the range: a-z
[^a-z]A character not in the range: a-z
[a-zA-Z]A character in the range: a-z or A-Z
[[:alnum:]]Letters and digits
[[:alpha:]]Letters
[[:ascii:]]Ascii codes 0-127
[[:blank:]]Space or tab only
[[:cntrl:]]Control characters
[[:digit:]]Decimal digits
[[:graph:]]Visible characters (not space)
[[:lower:]]Lowercase letters
[[:print:]]Visible characters
[[:punct:]]Visible punctuation characters
[[:space:]]Whitespace
[[:upper:]]Uppercase letters
[[:word:]]Word characters
[[:xdigit:]]Hexadecimal digits
//Meta sequences
\AStart of string
\zAbsolute end of string
\sAny whitespace character!
\SAny non-whitespace character
\dAny digit
\DAny non-digit
\wAny word character
\WAny non-word character
\bA word boundary
\BNon-word boundary
\GStart of match
\XAny unicode sequences
\CMatch one byte
\RUnicode newlines
\vVertical whitespace character
\VNegation of \v
\hHorizontal whitespace character
\HNegation of \h
\KReset match
\AStart of string
\ZEnd of string
\zAbsolute end of string
\nMatch nth subpattern
\pXUnicode property X
\p{...}Unicode property
\PXNegation of \p
\P{...}Negation of \p
\Q...\EQuote; treat as literals
\kMatch subpattern `name`
\k'name'Match subpattern `name`
\k{name}Match subpattern `name`
\gnMatch nth subpattern
\g{n}Match nth subpattern
\g{-n}Match nth relative subpattern
\g'name'Recurse subpattern `name`
\gRecurse nth subpattern
\g'n'Recurse nth subpattern
\g<+n>Recurse nth relative subpattern
\g'+n'Recurse nth relative subpattern
\xYYHex character YY
\x{YYYY}Hex character YYYY
\dddOctal character ddd
\cYControl character Y
[\b]Backspace character!
\Makes any character literal
re*Matches 0 or more occurrences of preceding expression.
re+Matches 1 or more occurrence of preceding expression.
re?Matches 0 or 1 occurrence of preceding expression.
ruby?Match "rub" or "ruby": the y is optional
ruby*Match "rub" plus 0 or more y
ruby+Match "rub" plus 1 or more y
\d{3}Match exactly 3 digits
\d{3,}Match 3 or more digits
\d{3,5}Match 3, 4, or 5 digits
Python(?=!)Match "Python", if followed by an exclamation point
Python(?!!)Match "Python", if not followed by an exclamation point
// my collection
[sS]teffen# shows steffen and Steffen in the text-file
^Steffen# Sentence starts with Steffen
Steffen$# sentence ends with Steffen
^Steffen$# only this word in the line
[0-9][0-9][0-9]$# ends with three numbers
[A-Z]{1,2}# word begin with one or two big letters
[A-Z][a-z]{2}# One big letter followed bei to small letters
^$# empty line
\.# all items with a point
\?# contains a question mark
^(file_.+)\.pdf$# finds: file_hsad.pdf file_28wssj.pdf; just print the name before .pdf
([A-Z][a-z]{2}\s(\d{4}))# Jan 1987 --> Jan 1987 1987
(\d{4})x(\d{3,4})# 1280x720 --> 1280 720
.*?# find link; *? --> lazy-quatifier
\s*<.*?>\s*# inner tags
href=\""(.*?)\""# href="abc123"
(?:\d+[a-z]|[a-z]+\d)[a-z\d]# string contain numbers and letters
(?<!\|)&(?!\|)# negative lookahead and lookbehind