user3526542
user3526542

Reputation: 25

Python regular expression to extract strings

Here is my test sentence:

in setup or operation state, the blood warmer shall declare an urgent state if its liquid leakage sensor detects liquid leakage.

I want extract:

setup or operation

note that there maybe two or more state in the sentence,Just find the first one as the boundary.

I test my code in regex101: https://regex101.com/r/p18Hef/1

(in (.*) (state|mode|phase|states))

but it couldn't find the first state...

Upvotes: 0

Views: 51

Answers (1)

wholevinski
wholevinski

Reputation: 3828

Regex is greedy by default. Your .* is matching all the way out to the next instance of "state", so you need to mark your .* as lazy with a ?.

The final regex is: (in (.*?) (state|mode|phase|states)).

More information can be seen at Regular expression operations.

Upvotes: 3

Related Questions