Re: Q on regex

Discussion in 'Python' started by MRAB, Aug 7, 2012.

  1. MRAB

    MRAB Guest

    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.
     
    MRAB, Aug 7, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    716
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,641
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    607
  4. Xah Lee
    Replies:
    1
    Views:
    951
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    781
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page