简体   繁体   中英

Regex? Match part of or whole word

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
string - "this is a sentence about a potato "
string - "this is a sentence about a potat "
string - "this is another sentence about a pota "

Thanks!

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',)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM