Issue
I need a regular expression to use in python that captures one group containing the whole string minus the last 2 characters, but if the string have a "-" , then ignore it and all after it.
Ex:
abcde = abc
jklmno-pqrs = jklm
I think it would be a mix between (.*)..$
and ^([^-]*)
, but I dont know how to combine them.
Solution
You could use a capture group matching any char except -
followed by matching 2 chars other than -
^([^-\n]+)[^-\n]{2}(?:-.*)?$
In parts, the pattern matches:
^
Start of string([^-\n]+)
Capture group 1, match 1 or more chars other than-
(add\n
to not match a newline)[^-\n]{2}
Match 2 chars other than-
(?:-.*)?
Optionally match-
followed by 0+ times any char$
End of string
For example
import re
pattern = r"^([^-\n]+)[^-\n]{2}(?:-.*)?$"
s = ("abcde\n""jklmno-pqrs")
print(re.findall(pattern, s, re.M))
Output
['abc', 'jklm']
Answered By - The fourth bird
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.