By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

Preg and why [^word) doesnt work?

P: n/a
Hi
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.


Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Juggernaut wrote:
Hi
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
[^ ] ?
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".
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?


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.

--
Oli

Jul 17 '05 #2

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

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.