Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26730

Re: Q on regex

Date 2012-08-07 18:12 +0100
From MRAB <regex@mrabarnett.plus.com>
Subject Re: Q on regex
References <1344336754.13108.2@numa-i>
Newsgroups comp.lang.python
Message-ID <mailman.3064.1344359523.4697.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Q on regex MRAB <regex@mrabarnett.plus.com> - 2012-08-07 18:12 +0100

csiph-web