开发者

.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.*开发者_如何学Go</td></tr>

I m new to regex...

Can any one tell me the way out.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜