Sal Sal - 4 years ago 260
Python Question

Regular Expression in Python to match multiple incidents of a string between two known strings

In the following text

My cow always gives milk. Your cow sometimes produces milk.


I want to extract

'always gives', 'sometimes produces'


Using the encapsulating strings "cow" and "milk".

I tried "cow(.*)milk" following Regular Expression to get a string between two strings in Javascript however it only works if the first sentence is alone

My cow always gives milk.


In my case, using that regular expression returns

always gives milk. Your cow sometimes produces


Additionally, I have also tried "(?<=foo)[^bar]*(?=bar)" from Extracting all values between curly braces regex php. And this works great. For example (and this is closer to the actual problem I'm trying to solve)

fooSTRINGbar fooCHARACTERSbar


Returns

'STRING', 'CHARACTERS'


Great! But for some reason if "STRING" contains a character that "bar" has, then the match fails. For example,

fooSTRaINGbar


Doesn't return anything.

Answer Source

Try the non-greedy option:

cow(.*?)milk

The ? qualifier tells the regex engine to stop a match at the earliest opportunity, rather than greedily trying to match as much as possible (which is the problem you saw with cow(.*)milk).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download