Regex Flashcards

1
Q

What import is needed to use regex?

A

import re

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

How does a basic regex in python looks like?

A

match = re.search(r’searchstring’,string)

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

Show an example of the following anchors?

  • Beginning of a string
  • End of a string
  • Word boundary
A

re. search(‘^SearchString’,string)
re. search(‘SearchString$’,string)
re. search(‘\bSearchString\b’,string)

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

How to search for special characters?

A

re.search(‘\?’,string)

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

How to define the regex in a variable?

A
pattern = r'Yves'
string1 = "Yves Sturzenegger"
match1 = re.search(pattern, string1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are character classes for:
any lowercase letter
any uppercase letter
any letter
hexadecimal values

A

r’[a-z]’
r’[A-Z]’
r’[a-zA-Z]’
r’[a-fA-F0-9]’

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

Regex example to check if only one char was entered?

A

r’^[a-zA-Z]$’

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

Write a pattern that matches any word that begins with b, followed by a vowel, and ending with t.

A

\bb[aeiou]t\b

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

Match any digit or carat symbol

Match line that does not contain numbers

Match any line containing a digit

Match any line containing a minus, zero or nine

A

r’[0-9^]’

r’[^0-9]’

r’[0-9]’

r’[-09]

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

What are the special character classes for:
[0-9] (digits)
[\t\r\n] (whitespaces)
[A-Za-z0-9_]

A

\d
\s
\w

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

What are the special character classes for:
[^0-9] (digits)
[^\t\r\n] (whitespaces)
[^A-Za-z0-9_]

A

\D
\S
\W

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

Regex should check only one of the following words item0, item1….

A

^item\d$

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

Find any three character?

A

r’\b…\b’

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

Find any line that contains only one character?

A

r’^.$’

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

Match any word that contains three lowercase letters?

A

r’\b[a-z]{3}\b’

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

5 ‘a’ characters
5 or more ‘a’ characters
between 5 and 7 ‘a’ characters
between 3 and 5 lowercase characters

A

r’a{5}’
r’a{5,}’
r’a{5,7}’
r’[a-z]{3,5}’

17
Q

What is the shorthand version of:
a{0,} - zero ore more
a{1,} - one or more
a{0,1} - zero or one

A

a*
a+
a?

18
Q

Find any number, whether it’s a whole number or a decimal

A

r’\d+.?\d*’

19
Q

Search for dog or cat or fish

A

r’dog|cat|fish’

20
Q

Search for cat or bat?

A

r’cat|bat’

r’[cb]at’

21
Q

Search for:
hack, crack, hacking, hacked, cracking, cracked

A

r’(hack|crack)(ing|ed)?’

22
Q

Search for IP address?

A

r’(\d{1,3}.){3}\d{1,3}’

23
Q

Search for credit card?

A

r’(\d{4} ){3}\d{4}’

24
Q

How to match groups?

A

r’([A-Za-z]+), (0-9]+)
all = match.group(0)
all = match.group()
name = match.group(1)
number = match.group(2)

25
Q

Replace occurrences of ‘H’ with ‘h’

A

text = re.sub(r’H’, r’h’, text)

26
Q

What is the difference between re.search() and re.findall()

A

re. search: finds first occurrence
re. findall: finds all occurrences