Reputation: 427
I am trying to determine if a sub-string exists within a text document of repetitive formatting. I am looping through specific keywords, and trying to identify another word after it. The two words are always separated by an integer, of varying value. I basically want a way to represent that integer in the sub-string as any integer value at all, if possible at all. Something like this:
substr = keyword +' '+ integer +' '+ word
teststr = "one two three keyword 24 word four five"
if substr in teststr:
print("substr exists in teststr")
Alternatively, I can do a loop and check around the iterator:
for el in teststr():
checkstr = keyword +' '+ el.isdigit +' '+ word
if checkstr in teststr:
print("yes")
Just wondering if anyone knows an elegant solution at the top of their head.
Upvotes: 0
Views: 110
Reputation: 3235
You can use Regular Expressions to capture that pattern. Here is a quick implementation of what you are looking for:
import re
sample = "one two three keyword 24 word four five, another test is here pick 12 me"
# (\w+) is a group to include a word, followed by a number (\d+), then another word
pattern = r"(\w+).(\d+).(\w+)"
result = re.findall(pattern, sample)
if result:
print('yes')
Upvotes: 1