Page 1 of 1

Matching any expression between tag A and B or tag C and B

Posted: Fri Jun 24, 2011 2:31 pm
by db579
I'm trying to use regex to return everything between two html tags where the first tag can be one of two things. So far I've attempted to do it like so:

Code: Select all

$pattern = "/<span class="Normal-[C,C0]">.+?<\/span>/";
preg_match_all($pattern,$targetstring,$match);
where I'm using the square brackets to indicate that I want it to match anything between a tag that matches <span class="Normal-C" or <span class="Normal-C0"> At the moment it only returns things matching the first scenario, can anyone point me in the right direction? Thanks

Re: Matching any expression between tag A and B or tag C and

Posted: Fri Jun 24, 2011 10:35 pm
by McInfo
Square brackets identify a character range. In your pattern, the character range tries to match one of four characters (actually three because the second C is redundant).

Code: Select all

[C,C0]
You might want a sub-pattern, which uses parentheses. Use a pipe symbol to separate two branches of a pattern (or sub-pattern) so that one of the alternatives will be matched.

Code: Select all

(C|C0)
Because the two alternatives share a common first letter, it could be written like this instead. The sub-pattern matches nothing or zero.

Code: Select all

C(|0)
A simpler way to write the pattern is with a plus sign which matches the character before it zero or one times.

Code: Select all

C0+
...Question mark matches the character before it zero or one times. (Thanks, Weirdan.)

Code: Select all

C0?

Re: Matching any expression between tag A and B or tag C and

Posted: Sat Jun 25, 2011 1:51 am
by Weirdan
A simpler way to write the pattern is with a plus sign which matches the character before it zero or one times.
Plus matches one or more times. In this case you would need question mark which would mark previous character as optional (match 0 or one time)

Re: Matching any expression between tag A and B or tag C and

Posted: Sat Jun 25, 2011 5:46 am
by db579
Thanks very much guys works perfectly.

Re: Matching any expression between tag A and B or tag C and

Posted: Sat Jun 25, 2011 10:39 am
by McInfo
Weirdan wrote:
A simpler way to write the pattern is with a plus sign which matches the character before it zero or one times.
Plus matches one or more times. In this case you would need question mark which would mark previous character as optional (match 0 or one time)
Oops... Thanks for catching that, Weirdan. I don't know what I was thinking.