What is this?
This is a collection of common Q&A. This is also a Community Wiki, so everyone is invited to participate in maintaining it.
Why is this?
[tag:regex] is suffering from give me ze code
type of questions and poor answers with no explanation. This reference is meant to provide links to quality Q&A.
What's the scope?
This reference is meant for the following languages: [tag:php], [tag:perl], [tag:javascript], [tag:python], [tag:ruby], [tag:java], [tag:.net].
This might be too broad, but these languages share the same syntax. For specific features there's the tag of the language behind it, example:
- What are regular expression Balancing Groups? [tag:.net]
Quantifiers:
- What does
+
mean? - What does
*
mean? - What does
?
mean? - What does
{n, m}
mean? - What does
{n,}
mean? - What does
{n}
mean? - What's the difference between
{n}
and{n}?
- Can someone explain Possessive Quantifiers to me? [tag:php], [tag:perl], [tag:java], [tag:ruby]
- What does
++
mean? - What does
*+
mean? - Emulating possessive quantifiers [tag:.net]
- Difference between
.*?
and.*
for regex: Greedy vs non-greedy, non-greedy also known as lazy, ungreedy or reluctant - Greedy vs. Reluctant vs. Possessive Quantifiers
Character classes:
- How does a character class
[]
work? - How does a negated character class
[^]
work? - How does a substraction in character classes
[range-[bar]]
work? [tag:java], [tag:ruby] 1.9+ - How does intersection in character classes
[range&&[bar]]
work? [tag:java], [tag:ruby] 1.9+ - How does POSIX character classes
[[:alpha:]]
work? - Why
[^\\D2]
,[^[^0-9]2]
,[^2[^0-9]]
get different results in Java? [tag:java]
Anchors:
- What does
^
mean? - What does
$
mean? - What does
\A
mean? [tag:php], [tag:perl], [tag:python], [tag:ruby] - What does
\Z
mean? [tag:php], [tag:perl], [tag:python], [tag:ruby] - What does
\G
mean? [tag:php], [tag:perl], [tag:ruby] - What does
\b
and\B
mean? word boundaries
Groups:
- What does a capturing group
()
mean? - What does a backreference
\1
mean?- Number after
\n
[tag:python]
- Number after
- What does a non capturing group
(?:)
mean? - What does a subpattern
(?i:regex)
mean? - What does an atomic group
(?>)
mean? - What does branch reset
(?|)
mean? - What does a named group mean?
(?P<groupname>regex)
[tag:python](?<groupname>regex)
[tag:.net](?<groupname>regex)
[tag:perl](?P<groupname>regex)
,(?<groupname>regex)
It has 2 syntaxes [tag:php]
Lookaround:
- What does a positive lookahead
(?=)
mean? - What does a negative lookahead
(?!)
mean? - What does a positive lookbehind
(?<=)
mean? Not supported by [tag:javascript] - What does a negative lookbehind
(?<!)
mean? Not supported by [tag:javascript]- Lookbehind limits in:
- Lookbehinds need to be zero-width [tag:php], [tag:perl], [tag:python], [tag:ruby]
- Limited length
{0,n}
[tag:java] - Variable length lookbehinds are allowed [tag:.net]
- Lookbehind alternatives:
- Using
\K
[tag:php], [tag:perl] - Alternative regex module for python [tag:python]
- The hacky way
- Mimicking Lookbehind in JavaScript External link, someone got one from SO?
- Using
- Lookbehind limits in:
Modifiers:
- What does the
i
modifier mean? - What does the
m
modifier mean? [tag:php] [tag:perl] [tag:python] [tag:javascript] [tag:.net] [tag:java]- What does the
m
modifier mean in Ruby? [tag:ruby]
- What does the
- What does the
s
modifier mean? Not supported by [tag:javascript] [tag:ruby] - What does the
x
modifier mean? - What does the
u
modifier mean? - What does the
e
modifier mean? [tag:php] - What does the
S
modifier mean? [tag:php] - What does the
U
modifier mean? - What does the
o
modifier mean? [tag:ruby] - What are inline modifiers?
Escape sequence:
- What does
\d
mean? - What does
\D
mean? - What does
\w
mean? - What does
\W
mean? - What does
\s
mean? - What does
\S
mean? - What does
\h
mean? - What does
\v
mean? - What does
\t
mean? - What does
\r
mean? - What does
\n
mean? - What does
\e
mean? - What does
\R
mean? [tag:php]
Other: not sure about the name ...
Control verbes: [tag:php] [tag:perl]
- What does
(*PRUNE)
mean? - What does
(*SKIP)
mean? - What does
(*FAIL)
/(*F)
mean? - What does
(*BSR_ANYCRLF)
mean? [tag:php]
Recursion: [tag:php] [tag:perl]
Advanced regex-fu
- match a string not containing a word
- How does this regex find triangular numbers?
- How to determine if a number is a prime with regex?
- How does this PCRE pattern detect palindromes?
- How can we match a^n b^n with Java regex?
- Match nested brackets
- Using a recursive pattern [tag:php], [tag:perl]
- Using balanced matching groups [tag:.net]
- “vertical” regex matching in an ASCII “image”
- Match strings whose length is a fourth power
- Verbs that act after backtracking and failure
- Match a number
- Extract host/port combo with .net regex - port part optional
Should I leave comments or edit directly? if the latter: How?