user
user

Reputation: 105

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!

Upvotes: 1

Views: 3199

Answers (5)

crackmigg
crackmigg

Reputation: 5881

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

Upvotes: 1

Phil H
Phil H

Reputation: 20131

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.

Upvotes: 0

jkitchen
jkitchen

Reputation: 1060

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.

Upvotes: 0

sberry
sberry

Reputation: 131968

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

Upvotes: 3

David
David

Reputation: 6571

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

Upvotes: 0

Related Questions