简体   繁体   中英

Match everything after particular pattern except whitespace till next letter

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"

You can do this without using regex but by combining the split() and strip() functions :

s = 'Medicine Treatment:     Not applicable'
s.split('Medicine Treatment:')[1].strip()

Output :

'Not applicable'

如果你有一个字符串ss.strip()将是字符串的副本,两边没有空格, s.lstrip()只会从左边删除空格。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM