AtBS 7 Regex Flashcards

1
Q

Steps into creating regex matching?

A
  1. import re module
2. Create regex module with compile function
ie the d\d\d\-d\d\d\d
  1. Pass the string you want into the regex search method
  2. Call the match object group to return a string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Example of phone number regex matching?

A

import re

phoneNumRegex=re.compile(r’\d\d\d-\d\d\d-\d\d\d\d’)

mo=phoneNumRegex.search(‘My number is 615-555-3244’)

print(‘Phone number found:’+mo.group())

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

What is a pipe and what does it do for regex?

A

It is the | symbol

It is the or statement for regex.

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

What does ? do in regex?

A

It flags the group that comes before it as an optional part to match.

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

What does * do in regex?

A

It is the match for zero or more character.

Can find haha or hahaha

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

What does + do in regex?

A

It is the match for one or more characters

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

What do curly brackets {} do in regex?

A

they match a specific number of instances.

ex (Ha){3} matches with HaHaHa but not HaHa

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

What is greedy versus nongreedy matching and how is it set?

A

greedy matching is the default state for the curly brackets with a range.

Example {3,5} will pick 5 repetitions unless you put question mark behind it.

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

\d is what in regex?

A

Any numeric digit from 0 to 9

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

\D is what in regex?

A

Any character that is not a numeric digit 0 to 9

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

\w is what in regex?

A

Any letter, numeric digit, or the underscore character

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

\W is what in regex?

A

Any character that is not a letter, numeric digit or the underscore character

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

\s is what in regex?

A

Any space, tab or newline character.

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

\S is what in regex?

A

Any character that is not a space, tab or a newline character.

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

Example of make your own regex to find vowels only?

A

vowelRegex=re.compile(r’[aeiouAEIOU]’)

square brackets are the key

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

What does caret symbol ^ do for regex?

Example

A

It finds based on what is not in a list

’[^aeiouAEIOU] would find consonants

17
Q

What can the caret and dollar sign do in regex?

A

Can make it so you need to find the search term at the beginning with the ^

or at the end of the string with a dollar sign

18
Q

What is the dot symbol . used for in regex?

Example

A

It is used to match all characters

atRegex=re.compile(r’.at’)

would match cat, bat, hat

19
Q

What does .* match?

Example

A

It matches everything

nameRegex = re.compile(r ‘ FirstName: (.*)’)

will pull all the first names