Regex? Match part of or whole word

前端 未结 5 1954
遥遥无期
遥遥无期 2021-01-25 23:33

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

相关标签:
5条回答
  • 2021-01-25 23:48

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

    0 讨论(0)
  • 2021-01-25 23:57
    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',)
    
    0 讨论(0)
  • 2021-01-25 23:58
    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.

    0 讨论(0)
  • 2021-01-25 23:58

    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.
    
    0 讨论(0)
  • 2021-01-26 00:04

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

    0 讨论(0)
提交回复
热议问题