Wildcards for regexps?

S

ssecorp

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.
 
P

Paul McGuire

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
 
T

Timothy Grant

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.
 
A

Aahz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top