fork(6) download
  1. import re
  2. # import regex # if you like good times
  3. # intended to replace `re`, the regex module has many advanced
  4. # features for regex lovers. http://p...content-available-to-author-only...n.org/pypi/regex
  5. pattern = r'(\w+):(\w+):(\d+)'
  6. subject = 'apple:green:3 banana:yellow:5'
  7. regex = re.compile(pattern)
  8.  
  9. ######## The six main tasks we're likely to have ########
  10.  
  11. # Task 1: Is there a match?
  12. print("*** Is there a Match? ***")
  13. if regex.search(subject):
  14. print ("Yes")
  15. else:
  16. print ("No")
  17.  
  18. # Task 2: How many matches are there?
  19. print("\n" + "*** Number of Matches ***")
  20. matches = regex.findall(subject)
  21. print(len(matches))
  22.  
  23. # Task 3: What is the first match?
  24. print("\n" + "*** First Match ***")
  25. match = regex.search(subject)
  26. if match:
  27. print("Overall match: ", match.group(0))
  28. print("Group 1 : ", match.group(1))
  29. print("Group 2 : ", match.group(2))
  30. print("Group 3 : ", match.group(3))
  31.  
  32. # Task 4: What are all the matches?
  33. print("\n" + "*** All Matches ***\n")
  34. print("------ Method 1: finditer ------\n")
  35. for match in regex.finditer(subject):
  36. print ("--- Start of Match ---")
  37. print("Overall match: ", match.group(0))
  38. print("Group 1 : ", match.group(1))
  39. print("Group 2 : ", match.group(2))
  40. print("Group 3 : ", match.group(3))
  41. print ("--- End of Match---\n")
  42.  
  43. print("\n------ Method 2: findall ------\n")
  44. # if there are capture groups, findall doesn't return the overall match
  45. # therefore, in that case, wrap the pattern in capturing parentheses
  46. # the overall match becomes group 1, so other group numbers are bumped up!
  47. wrappedpattern = "(" + pattern + ")"
  48. wrappedregex = re.compile(wrappedpattern)
  49. matches = wrappedregex.findall(subject)
  50. if len(matches)>0:
  51. for match in matches:
  52. print ("--- Start of Match ---")
  53. print ("Overall Match: ",match[0])
  54. print ("Group 1: ",match[1])
  55. print ("Group 2: ",match[2])
  56. print ("Group 3: ",match[3])
  57. print ("--- End of Match---\n")
  58.  
  59. # Task 5: Replace the matches
  60. # simple replacement: reverse group
  61. print("\n" + "*** Replacements ***")
  62. print("Let's reverse the groups")
  63. def reversegroups(m):
  64. return m.group(3) + ":" + m.group(2) + ":" + m.group(1)
  65. replaced = regex.sub(reversegroups, subject)
  66. print(replaced)
  67.  
  68.  
  69. # Task 6: Split
  70. print("\n" + "*** Splits ***")
  71. # Let's split at colons or spaces
  72. splits = re.split(r":|\s",subject)
  73. for split in splits:
  74. print (split)
  75.  
Success #stdin #stdout 0.11s 10104KB
stdin
Standard input is empty
stdout
*** Is there a Match? ***
Yes

*** Number of Matches ***
2

*** First Match ***
Overall match:  apple:green:3
Group 1 :  apple
Group 2 :  green
Group 3 :  3

*** All Matches ***

------ Method 1: finditer ------

--- Start of Match ---
Overall match:  apple:green:3
Group 1 :  apple
Group 2 :  green
Group 3 :  3
--- End of Match---

--- Start of Match ---
Overall match:  banana:yellow:5
Group 1 :  banana
Group 2 :  yellow
Group 3 :  5
--- End of Match---


------ Method 2: findall ------

--- Start of Match ---
Overall Match:  apple:green:3
Group 1:  apple
Group 2:  green
Group 3:  3
--- End of Match---

--- Start of Match ---
Overall Match:  banana:yellow:5
Group 1:  banana
Group 2:  yellow
Group 3:  5
--- End of Match---


*** Replacements ***
Let's reverse the groups
3:green:apple 5:yellow:banana

*** Splits ***
apple
green
3
banana
yellow
5