1

im trying out the fuzzy function of the new regex module. in this case, i want there to find a match for all strings with <= 1 errors, but i'm having trouble with it

import regex

statement = 'eol the dark elf'
test_1 = 'the dark'
test_2 = 'the darc' 
test_3 = 'the black'

print regex.search('{}'.format(test_1),statement).group(0) #works

>>> 'the dark' 

print regex.search('{}'.format(test_1){e<=1},statement).group(0)

>>> print regex.search('{}'.format(test_1){e<=1},statement).group(0) #doesn't work 
                                          ^
SyntaxError: invalid syntax 

i have also tried

print regex.search('(?:drk){e<=1}',statement).group(0) #works
>>> 'dark'

but this . . .

print regex.search(('(?:{}){e<=1}'.format(test_1)),statement).group(0) #doesn't work
>>> SyntaxError: invalid syntax
2
  • 1
    after serach you need only one (. It should be so: print regex.search('(?:{}){e<=1}'.format(test_1)).group(0) Commented Jul 3, 2013 at 19:08
  • where do you put the string being searched? Commented Jul 3, 2013 at 19:16

1 Answer 1

1

In your first snippet, you forgot to put the {e<=1} in a string. In your final snippet, I think the problem is, that format tries to deal with the {e<=1} itself. So either you use concatenation:

print regex.search(test_1 + '{e<=1}', statement).group(0)

or you escape the literal braces, by doubling them:

print regex.search('{}{{e<=1}}'.format(test_1), statement).group(0)

This can then easily be extended to

print regex.search('{}{{e<={}}}'.format(test_1, num_of_errors), statement).group(0)
Sign up to request clarification or add additional context in comments.

2 Comments

i'm a little confused on how to make the error number a variable. lets say error = 2 ... it doesn't work when i try '{e<={}}'.format(error)
@draconisthe0ry I just realized, literal braces can be escaped by doubling them. I'll edit the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.