Question

Need a little help to construct a regular expression for this scenario:

Contents:

<b>IfThisIsPresent:</b>
<span style="font-family:Arial">
<button type='submit'>
<span class="button-gradient">
<span class="button-test">
Look: NeedToExtractThat
</span>

So: if string IfThisIsPresent: is in my string, I should extract what is right after Look: string.

I developed the following RegEx, but it's not working:

((?<=IfThisIsPresent:)Look:(.*))

I know Java Pattern class doesn't support if then else. So I'm using lookbehind to check if IfThisIsPresent exists, then extract what is after Look:.

Could someone help?

Appreciated!

Was it helpful?

Solution

This regex should work for you:

(?s)(?<=IfThisIsPresent:.{0,1000}Look:).*

it matched for me:

NeedToExtractThat </span>

If you also want to get rid of following tags like </span> then you could use this regex:

(?s)(?<=IfThisIsPresent:.{0,1000}Look:)[^<]*

which matches:

NeedToExtractThat

see demo here: http://fiddle.re/8ez1h (click java button)

OTHER TIPS

This should do the trick I believe:

(?=.*IfThisIsPresent:).*?Look:(.*)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top