Issue
This is my code:
input_text_l = "hjahsdjhas pal sahashjas"
regex= re.compile(r"\s*\¿?(?:pal1|pal2|pal)\s*\??") #THIS IS THE REGEX THAT DOES NOT WORK CORRECTLY
if regex.search(input_text_l):
not_tag = " ".join(regex_tag.split(input_text_l))
#print(not_tag)
else:
pass
And this is a simple diagram on how the regular expression should work.
I hope you can help me with this.
Solution
Don't do complicated things: \s+…\s+
is sufficient. +
is actually exactly what you wrote: "one or more but not none"
import re
re.search('\s+pal\s+', input_text_l)
or for several patterns:
re.search('\s+(?:pal1|pal2|pal)\s+', input_text_l)
Example:
>>> input_text_l = "hjahsdjhas pal sahashjas"
>>> re.search('\s+(?:pal1|pal2|pal)\s+', input_text_l)
<re.Match object; span=(10, 15), match=' pal '>
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.