Issue
I need the regex expression to find a substring (it's a variable) that is either preceded/followed by punctuation, whitespaces or the start/end of the string. I don't know the size or content of the substring. I've come up with [\?\.!\- ]1abc[\?\.!\- ]
(this is a specific example where the substring is 1abc) but I don't know how to add start/end as a possibility. With the following list:
- "1abc"
- "2131abc2411abc"
- "Hausstrasse 1abc"
- "Parkallee 1abc "
- "1abc-2"
- "abc-def-1abc!"
I'd want the matches to be on all lines but not "2131abc2411abc". Alternatively I tried the pattern [\?\.!\- ]*1abc[\?\.!\- ]*
but with this, "2131abc2411abc" returns matches also.
Could someone help me out?
Solution
Directly use (^|[\?\.!\- ])
to match one of the boundary characters or the start of the string. For the end, use $
.
In addition, directly use 1abc
to match that substring literally rather than putting it into a character class which matches only one character from the set.
re.search(r'(^|[\?\.!\- ])1abc([\?\.!\- ]|$)', s)
Answered By - Unmitigated
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.