Elegant way of extracting substrings matching regex?

723 Views Asked by At

Is there a nice way in Python to do:

  • Check a String matches a set of regular expressions
  • If yes: get the matching parts back as tuples.

So essentially I want a simple way to enter simple parser/scanner grammars, and simply extract all matching in a certain structure (e.g. tuples)

So suppose we have encoded in a String a country code, an city name and an index. We want to extract this:

input = "123-NEWYORK-[2]"
grammar = "<country,[0-9]+>-<city,[A-Z]*>-[<index,[0-9]*>"
res = HOW_TO_DO_THIS(input,grammar)
if res is None:
  print("Does not match")
else
  (countrycode,city,index) = res
2

There are 2 best solutions below

0
On BEST ANSWER

With python3 you can do, note that the regex has been modified:

import re
input = "123-NEWYORK-[2]"
grammar = r"(?P<country>[0-9]+)-(?P<city>[A-Z]*)-(?P<index>\[[0-9]*\])"
res = re.findall(grammar, input)
if not res:
  print("Does not match")
else:
  (countrycode,city,index) = res[0]
  print(countrycode)

Modifications:

  • The correct regex would be (?P[0-9]+)-(?P[A-Z])-(?P[[0-9]])
  • The syntax for regex module in python is re.findall(patter, input_string). Not the opposite.
  • if not x is easier (and more generic) than if x is None
2
On

Check out this code. This is just for simple text lookup but you can extend according to your scenario

import re
f=open('sample.txt',"w")
f.write("<p class = m>babygameover</p>")
f.close()
f=open('sample.txt','r')
string = "<p class = m>(.+?)</p>" # regular expression
pattern = re.compile(string) # compiling
text = f.read()
search = re.findall(pattern,text) # searching 
print search