I am trying to figure out a regular expression for the following:
<tr class="A">.*</tr><tr class="(B|C)">.*</tr>
Now The second tr class will repeat an unknown number of times, with something unknown in between repetitions, but simply putting it in parentheses and added a plus doesn’t work.
Here’s the PHP code that didn’t work:
$pattern = '/<tr\ class=\"A\">.*(<tr\ class=\"(B|C)\">.*<\/tr>.*)+/';
preg_match_all($pattern,$playerHtml,$scores);
But it only returns the first
Here’s an example of something that should match:
<tr class="A">blah</tr>blah
<tr class="B">blah</tr>blah
<tr class="B">blah</tr>blah
<tr class="C">blah</tr>
This only matches blahblahblah
For your particular example, this regex will do:
Hope you can tweak it if need be. See the codepad demo here.
I needed to include
\nnewline characters for it to work.Because they are TR elements outside of TABLE elements, I had a hard time seeing the result of the preg_match_all function (because my browser immediately stripped the random TR elements). You may have had similar problems. I used htmlspecialchars() in the demo to output the regex match.
Also, it’s improper to have text between two TR elements:
<tr></tr>blah<tr></tr>So you should be careful about doing that.