regex Flashcards

1
Q

Digit

regex

2 alternatives

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

a or b

regex

A
(a|b)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Group

regex

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

Range (a or b or c)

regex

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

Not (a or b or c)

A
[^abc]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Lower case letter from a to q

A
[a-q]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Upper case letter from A to Q

A

[A-Q]

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

Digit from 0 to 7

A

[0-7]

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

Look-ahead assertions

A

?=

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

Negative look-ahead

A

?!

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

Look-behind assertion

A

?<=

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

Negative look-behind

A

?!=
?<!

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

Any character except new line

A

.

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

0 or more

A

*

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

1 or more

A

+

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

Exactly 3

A

{3}

17
Q

3 or more

A

{3,}

18
Q

3, 4, or 5

A

{3-5}

19
Q

Not digit

A

\D

20
Q

Word

A

\w

21
Q

Not word

A

\W

22
Q

White space

A

\s

23
Q

Not white space

A

\S

24
Q

Start of string or line

A
25
Q

Start of string

A

\A

26
Q

End of string or line

A

$

27
Q

End of string

A

\Z

28
Q

Word boundary

A

\b

29
Q

Not word boundary

A

\B

30
Q

Octal character xxx

A

\xxx

31
Q

Hex character hh

A

\xhh

32
Q

Use regular expressions

A

import re

matches = re.findall(r’\b\d+\b’, text)

33
Q

Cleaning adresses

A

import re

def clean_address(address):
pattern = r’\d+\s+(?:[A-Z][a-z]+\s+)+’ # Match street address part
match = re.search(pattern, address)
if match:
return match.group(0).title() # Return it capitalized
else:
return address # Leave it as is if no match