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? - 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? - What does a non capturing group
(?:)
mean? - What does an atomic group
(?>)
mean? - What does branch reset
(?|)
mean? - How does named groups work ? comment: since there are differences, we need to split this up
- [tag:php]
(?P<groupname>regex)
,(?<groupname>regex)
- [tag:python]
(?P<groupname>regex)
- [tag:.net] ...
- [tag:perl] ...
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: (something about fixed length)
- [tag:php], [tag:perl], [tag:python], [tag:ruby]
- [tag:java] (java is a bit more powerful AFAIK)
- [tag:.net] has no limits?
- 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 on SO?
- Using
- Lookbehind limits in: (something about fixed length)
Modifiers:
- What does the
i
modifier mean? - What does the
m
modifier mean? - What does the
s
modifier mean? - What does the
x
modifier mean? - What does the
e
modifier mean? [tag:php] - 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
\H
mean? - What does
\v
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 ...
- What does
|
mean? - What does
.
mean? - What special characters must be escaped?
Control verbes: [tag:php] [tag:perl]
- What does
(*PRUNE)
mean? - What does
(*SKIP)
mean? - What does
(*FAIL)
/(*F)
mean? - What does
(*COMMIT)
mean? - What does
(*MARK)
mean? - What does
(*THEN)
mean? - What does
(*ACCEPT)
mean?
Recursion: [tag:php] [tag:perl]
- What does
(?R)
mean? - What does
(?0)
,(?1)
mean? - What does
(?-1)
mean? - What does
(?P>groupname)
/(?&groupname)
mean?
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