Issue
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
Solution
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)
Answered By - Martin Ender
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.