Aug 19, 2015

Regex - Match only first occurence

In UTF-8 HTML text like this :

<table>
<tr class="dummy">
First
</tr>
<tr class="foo">
Second
</tr>
</table>
; you want to parse out list of two rows.

First,
Second

To achieve this use non greedy expression ?. Basically after you state what is pattern and how often it occurs by using ? you want to limit search only to first occurence.

Example regex with UTF8 greedy expression:

(?<=tr\sclass="\w+">)+?(?>\P{M}\p{M}*)+?(?:\<\/tr\>)+?
http://stackoverflow.com/questions/2503413/regular-expression-to-stop-at-first-match

1 comment:

  1. This UTF-8 HTMLcoding is very useful to me. Thanks for sharing this coding. Beacon App Development

    ReplyDelete