import re # import regex # if you like good times # intended to replace `re`, the regex module has many advanced # features for regex lovers. http://p...content-available-to-author-only...n.org/pypi/regex subject = 'Jane"" ""Tarzan12"" Tarzan11@Tarzan22 {4 Tarzan34}' regex = re.compile(r'{[^}]+}|"Tarzan\d+"|(Tarzan\d+)') # put Group 1 captures in a list matches = [group for group in re.findall(regex, subject) if group] ######## The four main tasks we're likely to have ######## # Task 1: Is there a match? print("*** Is there a Match? ***") if len(matches)>0: print ("Yes") else: print ("No") # Task 2: How many matches are there? print("\n" + "*** Number of Matches ***") print(len(matches)) # Task 3: What is the first match? print("\n" + "*** First Match ***") if len(matches)>0: print (matches[0]) # Task 4: What are all the matches? print("\n" + "*** Matches ***") if len(matches)>0: for match in matches: print (match) # Task 5: Replace the matches def myreplacement(m): if m.group(1): return "Superman" else: return m.group(0) replaced = regex.sub(myreplacement, subject) print("\n" + "*** Replacements ***") print(replaced) # Task 6: Split # Start by replacing by something distinctive, # as in Step 5. Then split. splits = replaced.split('Superman') print("\n" + "*** Splits ***") for split in splits: print (split)
Standard input is empty
*** Is there a Match? ***
Yes
*** Number of Matches ***
2
*** First Match ***
Tarzan11
*** Matches ***
Tarzan11
Tarzan22
*** Replacements ***
Jane"" ""Tarzan12"" Superman@Superman {4 Tarzan34}
*** Splits ***
Jane"" ""Tarzan12""
@
{4 Tarzan34}
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!