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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

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

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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?
Last edited by McInfo on Sat Jun 25, 2011 10:38 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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)
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

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

Post by db579 »

Thanks very much guys works perfectly.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Post Reply