Regex to find strings containing substring, but not ending on same substring
I'm trying to write a regex that checks if a string contains the substring "ing", but it most not end on "ing".
So the word sing would not work but singer would.
I think I have figured out how to make sure that the string does not end with ing, for that I'm using
But I can't seem to get it to work when I want the word to contain "ing" as well. I was thinking something like
But that does not work, all of my solution that sort of makes it work will take in more than one word as well. So it will match singer but not singer crafting, it should still match singer here, just not crafting.
You may use the pattern:
This would only be true for words which contain
ingwhich is also followed by another word character. Here is an example:
inp = 'singer' if re.search(r'ing(?=\w)', inp): print('singer is a MATCH') inp = 'sing' if re.search(r'ing(?=\w)', inp): print('sing is a MATCH')
singer is a MATCH
To match entire words containing non terminal
ing, I suggest using
inp = "Madonna is a singer who likes to sing." matches = re.findall(r'\b\w*ing\w+\b', inp) print(matches) # prints ['singer']
If the word can not end with ing but must contain ing:
\bA word boundary
\w*Match 0+ word characters
ingMatch the required
(?!\w*ing\b)Negaetive lookahead, assert the
ingis not at the end of the word
\w+Match 1+ word chars so that there must be at least a single char following
import re items = ["singer","singing","ing","This is a ing testing singalong"] pattern = r"\b\w*ing(?!\w*ing$)\w+\b" for item in items: result = re.findall(pattern, item) if result: print(result)
You can use this pattern:
import re pattern = re.compile('\w*ing\w+') print(pattern.match('sing')) # No match print(pattern.match('singer')) # Match