Match everything after particular pattern except whitespace till next letter

Felix

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"

Wiktor Stribiżew

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.

edited at
0

Comments

0 comments
Login to comment

Related

Split Everything before a particular pattern

Regex - how to match everything except a particular pattern

Regular expression: Match everything after a particular word

Remove everything except a certain pattern

Match everything except a specific string

Match everything except a pattern and replace matched with string

Regex to match everything except this regex

match everything up to first '/' after a specific pattern

Match everything except my pattern (RegEx)

Regex - greedy till next match

Regex Match everything except words of particular flag format

How can I grep everything except the pattern and its next n lines?

Match everything until a pattern

Google sheet : REGEXREPLACE match everything except a particular pattern

How to match everything except a particular pattern after/before a specific string constant

Regex: Match everything except backreference

preg match everything except a letter

Regex in JMeter - To capture a particular field after a pattern match

Print lines after a pattern match until next pattern match Python

Match a particular letter and print word after that using SED

Grep next word after pattern match

Is there a regex to remove everything after comma in a string except first letter

Regex to match everything except trailing pattern

Match everything except a complex regex pattern and replace it in Pandas

RegExp: match everything till next occurrence

Replacing everything with a backslash till next white space

Regex to match everything after a pattern occurrence until the next pattern occurs and so on

Pattern to match everything except a string of 5 digits

Match everything except certain strings