Regular Expression Syntax Flashcards

Python

You may prefer our related Brainscape-certified flashcards:
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
Q

\r\n

A

Line separator on Windows

26
Q

+

A

The + (one or more) is “greedy”

27
Q

?

A

Makes quantifiers “lazy”

28
Q

*

A

The * (zero or more) is “greedy”

29
Q

?

A

Makes quantifiers “lazy”

30
Q

{2,4}

A

Two to four times, “greedy”

31
Q

?

A

Makes quantifiers “lazy”

32
Q

[ abc ]

A

One of the characters in the brackets

33
Q

-

A

Range indicator

34
Q

[x-y]

A

One of the characters in the range from x to y

35
Q

[^x]

A

One character that is not x

36
Q

[^x-y]

A

One of the characters not in the range from x to y

37
Q

[\d\D]

A

One character that is a digit or a non-digit

38
Q

[\x41]

A

Matches the character at hexadecimal position 41 in the ASCII table, i.e. A

39
Q
A

Start of string or start of linedepending on multiline mode. (But when [^inside brackets], it means “not”)

40
Q

$

A

End of string or end of linedepending on multiline mode. Many engine-dependent subtleties.

41
Q

\A

A

Beginning of string (all major engines except JS)

42
Q

(?i)

A

Case-insensitive mode (except JavaScript)

43
Q

(?s)

A

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
Q

(?m)

A

Multiline mode (except Ruby and JS) ^ and $ match at the beginning and end of every line

45
Q

(?x)

A

Free-Spacing Mode mode (except JavaScript). Also known as comment mode or whitespace mode

46
Q

(?P…)

A

Python named group named bar

47
Q

(?=…)

A
Positive lookahead  
(?=\d{10})\d{5}
48
Q

(?<=…)

A

Positive lookbehind

(?<=\d)cat

49
Q

(?!…)

A

Negative lookahead

(?!theatre)the\w+

50
Q

(?<!…)

A

Negative lookbehind

\w{3}(?<!mon)ster

51
Q

(?(A)X|Y)

A

If proposition A is true, then match pattern X; otherwise, match pattern Y.

52
Q

(?(1)foo|bar)

A

If Group 1 has been set, the engine must match the literal characters foo. If not, it must match the literal characters bar

53
Q

(?(foo)…|…)

A

Check whether the Group named foo has been set.

54
Q

(?(-1)X|Y)

A

If the nearest capture group to the left of this conditional has been set, match pattern X; otherwise, match pattern Y.

55
Q

Alternation behavior

A

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).