View Full Version : regular expression

05-02-2009, 09:30 AM
Ok, i've been trying to learn regular expressions.


is the first i have wrote from scratch, it finds header tgs and paragraph tags (with no atributes)

<h3>Commercial & Domestic</h3>

it would pick up.

Problem is

<h3>Commercial &


Commercial & Domestic

It would not pick up

anyone know of a way to help?

05-02-2009, 10:23 AM
Your regexp seems very ineffecient.. I made this up, and the flag "s" treats the string as a single line.


$string = <<<HTML

preg_match_all('#<(?:h[1-6]|p)>(.*?)</(?:h[1-6]|p)>#is', $string, $matches);


05-02-2009, 10:47 AM
Right, could you explain to me is PHP regular expression different to other regular expression? for instance, i've downloaded a program called Expresso code project to test regular expressions. I copyied and paste what you put into there and tested and came up with no results. (apart from when i took away (# and #is, but then i was where i was before)

05-02-2009, 10:53 AM
Also, you used ?: when i want to match that as i want to know what data came from what tag.

05-02-2009, 11:53 AM
Okay well if you want to match what tag it is simply remove the ?: like you said on the first one. Also I've never used Expresso Code Project, and I can't even manage to get your regular expression to work in PHP by simply adding decimeters like // (or ##, @@, etc). So it's possible that your Expresso Code Project tool is making incompatible regular expressions or you are. Develop them in PHP and test them in PHP. Unless you're using ereg? Seriously I would just scrap your last one and stick with the one I made although it's not perfect it's not as messy and inefficient as yours. Personally I don't recommend using a program to make regexp so if you are, I'd reconsider.

05-02-2009, 12:32 PM
what i cant seem to get to work is the () (?($1)do this|or this)

Is this right or have i ot it round my neck?

if submatch $1 is set then do this.

thats what i want.

05-02-2009, 09:17 PM
No that is wrong, if you want to use something you matched prior in the regular expression you use \n for single quotes or \\n for double quotes (where n is a number) e.g.

preg_match_all('#<(h[1-6]|p)>(.*?)</\1>#is', $string, $matches);

So whatever they matched first must be used later and you can't do <h1>Header</p>