ScottFree
ScottFree

Reputation: 632

Regex matching just

Dumb beginner style question but I've wrestled with this for a while now, searches result is not quite what I'm after so I'm asking you guys instead!

Given the text below I only want to extract the Id associated with the bottom line (ie 100)

<test viewId=101&somemoreStuff=999>Dont match this</test>
<test viewId=100&somemoreStuff=111>Match this</test>

so I started with something like this (regular expression):

/viewId=(.+?)&

This captures the Id for the first line but I'm after the Id of the second line so I need to include the string "Match this" to distinguish between the two, something like this (pseudo code):

/viewId=(.+?)&[ignore this section]>Match this<

Thanks!

Upvotes: 0

Views: 42

Answers (2)

nu11p01n73R
nu11p01n73R

Reputation: 26667

Postive look aheads will be helpfull

viewId=(.+?)(?=&.*Match this)

Regex Example

  • (?=&.*Match this) postive look ahead. Asserts that the viewId is followed by Match this

Upvotes: 1

vks
vks

Reputation: 67968

viewId=(.+?)&[^>]*>Match this<

You can try this.See demo.

http://regex101.com/r/yR3mM3/46

Upvotes: 1

Related Questions