cfabio

Regex.py

Jan 20th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. import re
  2. message = 'Call me at the nuber 435-333-3333 tomorrow, or at 432-231-3433'
  3.  
  4. phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-d\d\d\d')
  5. mo = phoneNumRegex.search(message)   #stops at the first occurence. Returns some find regarding the found match
  6. mo = phoneNumRegex.findAll(message)  #finds all of them
  7.  
  8. #mo is None if no maches have been found
  9. print(mo.group())  #search and return the matches
  10.  
  11.  
  12. #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
  13. message = 'Call me at the nuber 435-333-3333 tomorrow'
  14. phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-d\d\d\d)')  
  15. mo = phoneNumRegex.search(message)   #stops at the first occurence. Returns some find regarding the found match
  16. print(mo.group())   #search and return the matches
  17. mo.group(1)
  18. mo.group(2)
  19.  
  20.  
  21. #search using the pipe for
  22. batRegex re.compile(r'Bat(man|mobile|copter')
  23. mo.group(1)     #to know which one among the different keywords match the string
  24.  
  25. #####################
  26. ####### Repetion ####
  27. #####################
  28.  
  29. #match a specific number of repetions with ? * +. All these special characther should be escaped if we need them as part of the path
  30. batRegex = re.compile(r'Bat(wo)?man')  #wo can appear 0 or 1 time
  31. phoneNumRegex = re.compile(r'(\d\d\d)?-\d\d\d-d\d\d\d')
  32.  
  33. batRegex = re.compile(r'Bat(wo)*man')  #wo can appear 0 or more time
  34. batRegex = re.compile(r'Bat(wo)+man')  #wo can appear 1 or more time
  35.  
  36. batRegex = re.compile(r'Bat(wo){3}man')  #wo can appear 1 or more time
  37. message = 'My nubers are 435-333-3333,432-231-3433,432-231-3433'
  38. 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
  39.  
  40. batRegex = re.compile(r'Bat(wo){3,5}man')  #wo can appear 3 to 4 times
  41. batRegex = re.compile(r'Bat(wo){3,}man')   #wo can appear 3 or more times
  42.  
  43.  
  44. sMessage = '123456789'
  45. digitRegex = re.compile(r'(\d){3,5})')   #greedy match by default. group() will match 12345
  46. digitRegex = re.compile(r'(\d){3,5}?)')  #non greedy match. It will return 123 as a match
  47.  
  48.  
  49. #############################
  50. ####### Search VS FinaAll ###
  51. #############################
  52.  
  53. mo = phoneNumRegex.search(message)   #returns a search object
  54. mo = phoneNumRegex.findAll(message)  #if there are no groups returns a list with the matched patterns
  55.                                      #if there are groups then it returns a list of tuples, each tuple contains what is matched per each group;
  56.                                      #it doesn't return the matches  
  57.  
  58.  
  59. #############################
  60. ####### Character classes ###
  61. #############################
  62.  
  63. #\d, \D, \w, \W, \s, \S -> character classes
  64. #search for all the words which are folloing a number
  65. re.compile(r'\d+\s\w+')
  66.  
  67. #[aiou] -> custom character class
  68. mo = re.compile(r'[aeiou]')  #match whatever of this letters. Same as r('a|e|i|o|u')
  69. mo = mo.findAll(message)     #returns list of matching vowels
  70.  
  71. re.compile(r'[a-fA-F]')
  72. re.compile(r'[ˆaeiou]')  #match whatever is not one of these letters
  73.  
  74. re.compile(r'ˆHello')  # match if the string begins with hello
  75. re.compile(r'World$')  # match if the string begins with hello
  76.  
  77. #############################
  78. ####### Match whater char ###
  79. #############################
  80.  
  81. #whatever character but a new line
  82. re.compile(r'.at')  #matches lat, rat, fat...
  83.  
  84. message = r'<To serve humans> for dinner.>'
  85. mo = re.compile(r'<(.*)>')  #the match is To serve humans> for dinner.
  86. mo = re.compile(r'<(.*?)>') #to have the non-greedy match, which will return To server humans
  87.  
  88. re.compile(r'.*',re.DOTALL) #with this option it will be matched whatever char, includig the new line
  89. mo = re.compile(r'[aeiou]', re.I)  #match whatever of this letters. Low or upper case because of the flag case insensitive
  90.  
  91. #############################
  92. ####### Find and replace  ###
  93. #############################
  94.  
  95. sMessage = 'Agent Fabio and Agent Elisa'  
  96. mo = re.compile(r'Agent \w+')
  97. mo.sub('Replacement', sMessage)  #automatically findall the matches of the pattern (Agent Fabio, Agent Elisa) and replacing them with the string "Replacement"
  98.  
  99. #matches in these case will be still (Agent Fabio, Agent Elisa) but the findall returns ony (F, E).
  100. #We return as match only the content of the group!!
  101. mo = re.compile(r'Agent (\w)\w*')  
  102. #replace Agent Fabio with Agent F*****, and Agent Elisa with Agent E*****
  103. #\1 indicates the match we have in the group1
  104. mo.sub(r'Agent \1****', sMessage)  
  105.  
  106. #to add comments or split the regular exp we can use verborse flag
  107. #with the verbose flag the spaces are not considered as part of the pattern
  108. #and we can also add comments
  109. re.compile(r'''
  110.     (\d\d\d-) |   #area code
  111.     (\d(\d\d\d\d) #comment1
  112.     \d\d\d''', re.VEROBSE)
  113.  
  114. #to pass multiple options we can use the bitwise OR operator
Advertisement
Add Comment
Please, Sign In to add comment