Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- message = 'Call me at the nuber 435-333-3333 tomorrow, or at 432-231-3433'
- phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-d\d\d\d')
- mo = phoneNumRegex.search(message) #stops at the first occurence. Returns some find regarding the found match
- mo = phoneNumRegex.findAll(message) #finds all of them
- #mo is None if no maches have been found
- print(mo.group()) #search and return the matches
- #use the paranthesis to group the different parts; if the paranthesis are part of the path we are looking for they must be back-slashed
- message = 'Call me at the nuber 435-333-3333 tomorrow'
- phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-d\d\d\d)')
- mo = phoneNumRegex.search(message) #stops at the first occurence. Returns some find regarding the found match
- print(mo.group()) #search and return the matches
- mo.group(1)
- mo.group(2)
- #search using the pipe for
- batRegex re.compile(r'Bat(man|mobile|copter')
- mo.group(1) #to know which one among the different keywords match the string
- #####################
- ####### Repetion ####
- #####################
- #match a specific number of repetions with ? * +. All these special characther should be escaped if we need them as part of the path
- batRegex = re.compile(r'Bat(wo)?man') #wo can appear 0 or 1 time
- phoneNumRegex = re.compile(r'(\d\d\d)?-\d\d\d-d\d\d\d')
- batRegex = re.compile(r'Bat(wo)*man') #wo can appear 0 or more time
- batRegex = re.compile(r'Bat(wo)+man') #wo can appear 1 or more time
- batRegex = re.compile(r'Bat(wo){3}man') #wo can appear 1 or more time
- message = 'My nubers are 435-333-3333,432-231-3433,432-231-3433'
- phoneNumRegex = re.compile(r'((\d\d\d-)?\d\d\d-d\d\d\d(,)?){3}') #match 3 phone numbers in a row, optionally separated with a comma, optionally having an area code
- batRegex = re.compile(r'Bat(wo){3,5}man') #wo can appear 3 to 4 times
- batRegex = re.compile(r'Bat(wo){3,}man') #wo can appear 3 or more times
- sMessage = '123456789'
- digitRegex = re.compile(r'(\d){3,5})') #greedy match by default. group() will match 12345
- digitRegex = re.compile(r'(\d){3,5}?)') #non greedy match. It will return 123 as a match
- #############################
- ####### Search VS FinaAll ###
- #############################
- mo = phoneNumRegex.search(message) #returns a search object
- mo = phoneNumRegex.findAll(message) #if there are no groups returns a list with the matched patterns
- #if there are groups then it returns a list of tuples, each tuple contains what is matched per each group;
- #it doesn't return the matches
- #############################
- ####### Character classes ###
- #############################
- #\d, \D, \w, \W, \s, \S -> character classes
- #search for all the words which are folloing a number
- re.compile(r'\d+\s\w+')
- #[aiou] -> custom character class
- mo = re.compile(r'[aeiou]') #match whatever of this letters. Same as r('a|e|i|o|u')
- mo = mo.findAll(message) #returns list of matching vowels
- re.compile(r'[a-fA-F]')
- re.compile(r'[ˆaeiou]') #match whatever is not one of these letters
- re.compile(r'ˆHello') # match if the string begins with hello
- re.compile(r'World$') # match if the string begins with hello
- #############################
- ####### Match whater char ###
- #############################
- #whatever character but a new line
- re.compile(r'.at') #matches lat, rat, fat...
- message = r'<To serve humans> for dinner.>'
- mo = re.compile(r'<(.*)>') #the match is To serve humans> for dinner.
- mo = re.compile(r'<(.*?)>') #to have the non-greedy match, which will return To server humans
- re.compile(r'.*',re.DOTALL) #with this option it will be matched whatever char, includig the new line
- mo = re.compile(r'[aeiou]', re.I) #match whatever of this letters. Low or upper case because of the flag case insensitive
- #############################
- ####### Find and replace ###
- #############################
- sMessage = 'Agent Fabio and Agent Elisa'
- mo = re.compile(r'Agent \w+')
- mo.sub('Replacement', sMessage) #automatically findall the matches of the pattern (Agent Fabio, Agent Elisa) and replacing them with the string "Replacement"
- #matches in these case will be still (Agent Fabio, Agent Elisa) but the findall returns ony (F, E).
- #We return as match only the content of the group!!
- mo = re.compile(r'Agent (\w)\w*')
- #replace Agent Fabio with Agent F*****, and Agent Elisa with Agent E*****
- #\1 indicates the match we have in the group1
- mo.sub(r'Agent \1****', sMessage)
- #to add comments or split the regular exp we can use verborse flag
- #with the verbose flag the spaces are not considered as part of the pattern
- #and we can also add comments
- re.compile(r'''
- (\d\d\d-) | #area code
- (\d(\d\d\d\d) #comment1
- \d\d\d''', re.VEROBSE)
- #to pass multiple options we can use the bitwise OR operator
Advertisement
Add Comment
Please, Sign In to add comment