Negating...

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

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Negating...

Post by Ree »

I want to match all tags which are not indicated tags. How do I do this? Say, the start of a tag:

Code: Select all

$pattern = '/<td.*?>/'is;
How do I match ANY starting tag which is not a td starting tag?
Last edited by Ree on Wed Oct 26, 2005 1:59 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$re = '/<(?!td)[^>]*>/is';
[^chars] is used to negate a character class - the characters dont have to come in the order you put them in the brackets though

(?!pattern) is a negative lookahead.

<(?!td) means any "<" that's NOT followed by td

;)

PS: Don't forgret to put the pattern in quotes in PHP, unlike perl and javascript :)
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Thanks, will try that. :)
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

And if I wanted to add some more tags besides td, say, tr, would it look like this?

Code: Select all

$pattern = '/<(?!(td|tr))[^>]*>/is';
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

I have tried this to remove any tags in the string that are not td or tr:

Code: Select all

$table = preg_replace('/<[\/]?(?!td|tr).*?>/is', '', $table);
For some reason it also deletes ending td and tr tags (</td>, </tr>). What is wrong with my regex?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

That's a very good question...and one that after much (drunken) consideration I came to this conclusion...

<[\/]? That'll match the T or the D, and so TD does not follow either....


;)
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

I did this and it works well now:

Code: Select all

$table = preg_replace('/<(?![\/]?(td|tr)).*?>/is', '', $table);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ree wrote:I did this and it works well now:

Code: Select all

$table = preg_replace('/<(?![\/]?(td|tr)).*?>/is', '', $table);
Very clever ;)
Post Reply