Regular Expression Syntax Flashcards

Python

1
Q

\d

A

Most engines: one digit from 0 to 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

\d

A

Python 3: one Unicode digit in any script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

\w

A

Most engines: “word character”: ASCII letter, digit or underscore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

\s

A

Most engines: “whitespace character”: space, tab, newline, carriage return, vertical tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

\D

A

One character that is not adigit as defined by your engine’s \d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

\W

A

One character that is not aword character as defined by your engine’s \w

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

\S

A

One character that is not awhitespace character as defined by your engine’s \s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

{3}

A

Exactly three times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

{2,4}

A

Two to four times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

{3,}

A

Three or more times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

*

A

Zero or more times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

?

A

Once or none

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

.

A

Any character except line break

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

.

A

A period (special character: needs to be escaped by a )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

\

A

Escapes a special character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

\

A

Escapes a special character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

|

A

Alternation / OR operand

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

( )

A

Capturing group

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

\1

A

Contents of Group 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

\2

A

Contents of Group 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

(?: )

A

Non-capturing group

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

\t

A

Tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

\r

A

Carriage return character

24
Q

\n

A

Line feed character

25
\r\n
Line separator on Windows
26
+
The + (one or more) is "greedy"
27
?
Makes quantifiers "lazy"
28
*
The * (zero or more) is "greedy"
29
?
Makes quantifiers "lazy"
30
{2,4}
Two to four times, "greedy"
31
?
Makes quantifiers "lazy"
32
[ abc ]
One of the characters in the brackets
33
-
Range indicator
34
[x-y]
One of the characters in the range from x to y
35
[^x]
One character that is not x
36
[^x-y]
One of the characters not in the range from x to y
37
[\d\D]
One character that is a digit or a non-digit
38
[\x41]
Matches the character at hexadecimal position 41 in the ASCII table, i.e. A
39
^
Start of string or start of linedepending on multiline mode. (But when [^inside brackets], it means "not")
40
$
End of string or end of linedepending on multiline mode. Many engine-dependent subtleties.
41
\A
Beginning of string (all major engines except JS)
42
(?i)
Case-insensitive mode (except JavaScript)
43
(?s)
DOTALL mode (except JS and Ruby). The dot (.) matches new line characters (\r\n). Also known as "single-line mode" because the dot treats the entire input as a single line
44
(?m)
Multiline mode (except Ruby and JS) ^ and $ match at the beginning and end of every line
45
(?x)
Free-Spacing Mode mode (except JavaScript). Also known as comment mode or whitespace mode
46
(?P...)
Python named group named bar
47
(?=…)
``` Positive lookahead (?=\d{10})\d{5} ```
48
(?<=…)
Positive lookbehind | (?<=\d)cat
49
(?!…)
Negative lookahead | (?!theatre)the\w+
50
(?
Negative lookbehind | \w{3}(?
51
(?(A)X|Y)
If proposition A is true, then match pattern X; otherwise, match pattern Y.
52
(?(1)foo|bar)
If Group 1 has been set, the engine must match the literal characters foo. If not, it must match the literal characters bar
53
(?(foo)…|…)
Check whether the Group named foo has been set.
54
(?(-1)X|Y)
If the nearest capture group to the left of this conditional has been set, match pattern X; otherwise, match pattern Y.
55
Alternation behavior
An alternation is expected to quit as soon as one of the alternatives matches, not hold out for the longest match. You can force it to continue by adding a condition after the alternation that can't be met until the whole token has been consumed. The simplest option would be an anchor ($) or a word boundary (\b).