I have a string like this:
'Medicine Treatment: Not applicable'
Now, I want to match the according treatment but without the white spaces infront of the treatment. So I just want to match: "Not applicable"
and not " Not applicable"
Im pretty sure it must be something like this:
(?<=Medicine Treatment:)[^\s].*
or (?<=Medicine Treatment:)\S.*
... (?<=Medicine Treatment:).*
returns only what I do not want: " Not applicable"
Python re
does not support patterns matching strings of unknown length (though it is possible with PyPi regex
module). So, you can't use re.search(r'(?<=Medicine Treatment:\s*).*', txt)
(it would work with PyPi regex.search
).
You may use a capturing group instead of a lookbehind:
import re
s = 'Medicine Treatment: Not applicable'
m = re.search(r'Medicine Treatment:\s*(.*)', s)
if m:
print(m.group(1)) # => Not applicable
See the Python demo
Details
Medicine Treatment:
- a literal string (serves as left-hand context of the required match)\s*
- consumes zero or more whitespace chars(.*)
- captures into Group 1 any 0 or more chars other than line break chars as many as possble.Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments