Wildcards for regexps?

Discussion in 'Python' started by ssecorp, Aug 11, 2008.

  1. ssecorp

    ssecorp Guest

    If I have an expression like "bob marley" and I want to match
    everything with one letter wrong, how would I do?
    so "bob narely" and "vob marley" should match etc.
     
    ssecorp, Aug 11, 2008
    #1
    1. Advertisements

  2. ssecorp

    Paul McGuire Guest

    On Aug 10, 11:10 pm, ssecorp <> wrote:
    > If I have an expression like "bob marley" and I want to match
    > everything with one letter wrong, how would I do?
    > so "bob narely" and "vob marley" should match etc.


    At first, I was going to suggest the brute force solution:

    ".ob marley|b.b marley|bo. marley|bob.marley|bob .arley|bob m.rley|bob
    ma.ley|bob mar.ey|bob marl.y|bob marle."

    But then I realized that after matching the initial 'b', later
    alternative matches wouldn't need to keep retesting for a leading 'b',
    so here is a recursive re that does not go back to match previously
    matched characters:

    ".ob marley|b(.b marley|o(. marley|b(.marley| (.arley|m(.rley|a(.ley|
    r(.ey|l(.y|e.))))))))"

    Here are some functions to generate these monstrosities:

    base = "bob marley"

    def makeOffByOneMatchRE(s):
    return "|".join(s[:i]+'.'+s[i+1:] for i in range(len(s)))
    re_string = makeOffByOneMatchRE(base)
    print re_string

    def makeOffByOneMatchRE(s,i=0):
    if i==len(s)-2:
    return '.' + s[-1] + '|' + s[-2] + '.'
    return '.' + s[i+1:] + '|' + s + '(' + makeOffByOneMatchRE(s,i
    +1) + ')'
    re_string = makeOffByOneMatchRE(base)
    print re_string


    -- Paul
     
    Paul McGuire, Aug 11, 2008
    #2
    1. Advertisements

  3. Diez B. Roggisch, Aug 11, 2008
    #3
  4. On Sun, Aug 10, 2008 at 9:10 PM, ssecorp <> wrote:
    > If I have an expression like "bob marley" and I want to match
    > everything with one letter wrong, how would I do?
    > so "bob narely" and "vob marley" should match etc.


    At one point I needed something like this so did a straight port of
    the double-metaphone code to python.

    It's horrible, it's ugly, it's non-pythonic in ways that make me
    cringe, it has no unit tests, but it does work.

    --
    Stand Fast,
    tjg. [Timothy Grant]
     
    Timothy Grant, Aug 11, 2008
    #4
  5. ssecorp

    Aahz Guest

    In article <>,
    ssecorp <> wrote:
    >
    >If I have an expression like "bob marley" and I want to match
    >everything with one letter wrong, how would I do?
    >so "bob narely" and "vob marley" should match etc.


    difflib
    --
    Aahz () <*> http://www.pythoncraft.com/

    Adopt A Process -- stop killing all your children!
     
    Aahz, Aug 12, 2008
    #5
    1. Advertisements

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. Fredrik Ramsberg

    Optimisation of regexps in Perl?

    Fredrik Ramsberg, Oct 14, 2003, in forum: Perl
    Replies:
    2
    Views:
    609
    Fredrik Ramsberg
    Oct 15, 2003
  2. Replies:
    4
    Views:
    789
  3. Spartanicus

    Help with Regexps wanted

    Spartanicus, Oct 21, 2004, in forum: HTML
    Replies:
    4
    Views:
    629
    William Park
    Oct 25, 2004
  4. Klaus Alexander Seistrup

    Expanding regexps

    Klaus Alexander Seistrup, Oct 28, 2003, in forum: Python
    Replies:
    5
    Views:
    514
    Klaus Alexander Seistrup
    Oct 29, 2003
  5. Kirk Strauser

    Problems with regexps

    Kirk Strauser, Nov 7, 2003, in forum: Python
    Replies:
    6
    Views:
    418
    Edward K. Ream
    Nov 8, 2003
  6. Magnus Lie Hetland

    Recursive regexps?

    Magnus Lie Hetland, Nov 19, 2004, in forum: Python
    Replies:
    4
    Views:
    806
    Jeremy Bowers
    Nov 20, 2004
  7. Stefan Rank
    Replies:
    1
    Views:
    350
    =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=
    Sep 14, 2005
  8. Ernst Elzas
    Replies:
    0
    Views:
    357
    Ernst Elzas
    Jan 15, 2006
Loading...