Reputation: 177
I have the following problem and just can't find a solution.
I have to find the exact start and end position of the following substring:
"hello world is a good idea for a T-shirt"
in any possible other String such as:
"This is some string, that includes commas, and other punctuations. It also includes hello world, is a, good, idea for a T-shirt and other."
due to the punctuations (commas) find() won't give me a result. I am trying to use regex such as r"(Hello)[\W+] (world) [\W+]..."
but it does not work either. Any good ideas?
Edit:
Here is my code:
import re
text = "This is some string, that includes commas, and other punctuations. It also includes hello world, is a, good, idea for a T-shirt and other."
match = re.search(r"[\W+](hello)[\W+](world)[\W+](is)[\W+](a)[\W+](good)[\W+](idea)[\W+](for)[\W+](a)[\W+](T-shirt)", text)
print (match)
Upvotes: 3
Views: 689
Reputation: 627410
When you use [\W+]
, you create a character class that matches a single character, either a non-word char (any char that is not a letter, digit or _
) OR a literal +
symbol.
Use \W+
instead of spaces:
import re
text = "This is some string, that includes commas, and other punctuations. It also includes hello world, is a, good, idea for a T-shirt and other."
match = re.search(r"hello\W+world\W+is\W+a\W+good\W+idea\W+for\W+a\W+T-shirt", text)
if match:
print("YES!")
See the Python demo
The \W
matches any char that is not a letter, digit or _
char and +
makes the regex engine match 1 or more occurrences of these chars.
To make the code more generic, you can split the initial string with space, and then join with regex pattern to match space or comma or dot.
import re
key = "hello world is a good idea for a T-shirt"
pat = r"\W+".join([re.escape(x) for x in key.split()])
# print(pat) # => hello\W+world\W+is\W+a\W+good\W+idea\W+for\W+a\W+T\-shirt
text = "This is some string, that includes commas, and other punctuations. It also includes hello world, is a, good, idea for a T-shirt and other."
match = re.search(pat, text)
if match:
print("YES!")
Upvotes: 1