Q on regex

MRAB regex at mrabarnett.plus.com
Tue Aug 7 13:12:06 EDT 2012


On 07/08/2012 11:52, Helmut Jarausch wrote:> Hi Matthew,
 >
 > how to fix the code below to match  'Hellmuth' instead of ' Hellmut' ?
 >
 > A negative look behind in front of the pattern doesn't help since it
 > counts
 > as an error. One would need a means to mix a required match with a
 > fuzzy match.
 >
 >
 > #!/usr/bin/python3
 > import regex
 >
 > Author= regex.compile(r'(?:Helmut){e<=2}')
 >
 > R=Author.search('Jarausch Hellmuth')
 > if R :
 >     print("matching string : |{0}|".format(R.group()))
 >     # matches  ' Hellmut'
 > else :
 >     print("nothing matched")
 >
There are two ways you could do it.

One way is to put word boundaries outside the fuzzy match:

     Author = regex.compile(r'\b(?:Helmut){e<=2}\b')

The other way is to use the 'ENHANCEMATCH' flag (or '(?e)' in the
pattern), which tells it to 'enhance' the match by reducing the number
of errors:

     Author = regex.compile(r'(?e)(?:Helmut){e<=2}')

 >
 > Many thanks for a hint,
 > Helmut.
 >
 > P.S. I've tried to initiate a discussion on adding your module to the
 > current standard library on C.L.P.
 >
The problem with adding it to the standard library is that any releases
would be tied to Python's releases and I would have much less leeway in
making changes. There are a number of other modules which remain
outside the standard library for just that reason.



More information about the Python-list mailing list