Skip to content
Advertisement

regex to catch text until a signal word occurs

I’m trying to create a regex which catches a text until a signal word occurs. Until the signal word is not the first word my solution works fine. Since I’m using python with the regex module the code is

JavaScript

And

JavaScript

becomes

JavaScript

But if the signal word is the first word it does not work properly. And

JavaScript

becomes

JavaScript

I want it to do nothing if the signal word is the first word. I’ve played with the regex.DOTALL and regex.MULTILINE parameter, but I had no positive match.

Advertisement

Answer

You might use a negative lookahead (?!SIGNALWORD) to assert that the string does not start with SIGNALWORD

JavaScript

See the outcome of the first Python demo and the second Python demo.

Advertisement