Preg and why [^word) doesnt work?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I was trying to write a script to replace some text inside some tags.

Lets say I had <tag stuff=stuff><tag stuff=otherstuff><another>

I wanted it to find all the <tag and remove them.

So I tried to write it so that it would go forward until it reached
[<^another] came. But when I tried that it would respond to another as
single characters and not as the word.

How can I do this with regular expressions, matching up against words using
[^ ] ?

I know I can do what Im trying to do other ways, but when it didnt work with
[^ ], it got me thinking about how to do it that way. Because there might
come other times when I want to 'NOT' against a word, and there has to be a
way of doing this with Regular expressions?

Thanks for any help you can give me.

Re: Preg and why [^word) doesnt work?

Juggernaut wrote:
Quoted text here. Click to load it

You can't. [xyz] means the set of characters "x", "y" or "z". [^xyz]
means the complement of this set, i.e. every character that isn't "x",
"y", or "z". Using either in a regex means "match against any member of
the corresponding set".

Quoted text here. Click to load it

Matching "all the things that aren't '<another>'" doesn't make much
sense. For instance, 'uff><anot' isn't '<another>', and you don't want
that removed, etc...

However, read the section on assertions in the section on Pattern
Syntax in the PHP manual, and see if that helps.

Otherwise, do it a different way. e.g.

    preg_replace('/<tag .*?>/', '', $str);

Which is just about the simplest way you can express this, I think.


Re: Preg and why [^word) doesnt work?

What you need is a lookahead assertion. The syntax if I remember
correctly is (?=<another).

Site Timeline