I was wondering if it\'s possible to use regex with python to capture a word, or a part of the word (if it\'s at the end of the string).
Eg:
target word - potato
Dont know how to match a regex in python, but the regex would be:
"\bp$|\bpo$|\bpot$|\bpota$|\bpotat$|\bpotato$"
This would match anything from p
to potato
if its the last word in the string, and also for example not something like "foopotato", if this is what you want.
The |
denotes an alternative, the \b
is a "word boundary", so it matches a position (not a character) between a word- and a non-word character. And the $
matches the end of the string (also a position).
import re
def get_matcher(word, minchars):
reg = '|'.join([word[0:i] for i in range(len(word), minchars - 1, -1)])
return re.compile('(%s)$' % (reg))
matcher = get_matcher('potato', 4)
for s in ["this is a sentence about a potato", "this is a sentence about a potat", "this is another sentence about a pota"]:
print matcher.search(s).groups()
OUTPUT
('potato',)
('potat',)
('pota',)
import re
patt = re.compile(r'(p|po|pot|pota|potat|potato)$')
patt.search(string)
I was tempted to use r'po?t?a?t?o?$'
, but that would also match poto or pott.
No, you can't do that with a regex as far as I know, without pointless (p|po|pot ...)
matches which are excessive. Instead, just pick off the last word, and match that using a substring:
match = re.search('\S+$', haystack)
if match.group(0) == needle[:len(match.group(0))]:
# matches.
Use the $
to match at the end of a string. For example, the following would match 'potato' only at the end of a string (first example):
"potato$"
This would match all of your examples:
"pota[to]{1,2}$"
However, some risk of also matching "potao" or "potaot".