Nag
Nag

Reputation: 11

.net shortest string regular expression

How to find shortest string, first occurance it should return

I have this string. I m looking for td whose value contains blabla with closing td. For ex:

  <tr blabla><td>blabla big content</td></tr><tr><td>thisisnot</td></tr>

I want only this string

  <tr blabla><td>blabla big content</td></tr>

I m using this regex in .net

<tr.*><td>blabla.*</td></tr>

I m new to regex...

Can any one tell me the way out.

Upvotes: 1

Views: 1027

Answers (2)

Xhalent
Xhalent

Reputation: 3954

Regex is by nature greedy - it will try and match the longest string that satisfies the pattern.

You need to use non-greedy quantifier in your pattern. So instead of "*" use "*?", and then use groupings to "capture" the match. The anonymous capturing of items is done by enclosing the group you want to capture in a set of parenthesis. The following seems to do the trick:

(<tr.*?><td>blabla.*?</td></tr>).*

This will create a capture group that you will need to query the regex result for.

Upvotes: 6

daalbert
daalbert

Reputation: 1475

Use (?<=<td>)[^<]+ as the regex, then do a length comparison on the matches.

Upvotes: 0

Related Questions