AtBS 7 Regex Flashcards
Steps into creating regex matching?
- import re module
2. Create regex module with compile function ie the d\d\d\-d\d\d\d
- Pass the string you want into the regex search method
- Call the match object group to return a string
Example of phone number regex matching?
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())
What is a pipe and what does it do for regex?
It is the | symbol
It is the or statement for regex.
What does ? do in regex?
It flags the group that comes before it as an optional part to match.
What does * do in regex?
It is the match for zero or more character.
Can find haha or hahaha
What does + do in regex?
It is the match for one or more characters
What do curly brackets {} do in regex?
they match a specific number of instances.
ex (Ha){3} matches with HaHaHa but not HaHa
What is greedy versus nongreedy matching and how is it set?
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.
\d is what in regex?
Any numeric digit from 0 to 9
\D is what in regex?
Any character that is not a numeric digit 0 to 9
\w is what in regex?
Any letter, numeric digit, or the underscore character
\W is what in regex?
Any character that is not a letter, numeric digit or the underscore character
\s is what in regex?
Any space, tab or newline character.
\S is what in regex?
Any character that is not a space, tab or a newline character.
Example of make your own regex to find vowels only?
vowelRegex=re.compile(r’[aeiouAEIOU]’)
square brackets are the key