negate a match in regex

I

ikeon

Hi All,
I have a script that I convert xml tags to html. like "<" I convert to
"&lt;" and so on.
after the conversion I need to capture the information inside the tag.
let take for example the string "&lt;abcd&gt;: which is equivalent to
"<abcd>".
I tried to capture the "abcd" which can be different from tag to tag
in the following way:

/\&lt\;([^\&\gt\;]*)/

like match "&lt;" and then match anything that is not "&gt;".
the thing is that it doesn't work on all tags for some reason and I
was wondering on a principal base if doing a [^somestring] suppose to
work ?

Thanks.
 
P

Peter Makholm

ikeon said:
like match "&lt;" and then match anything that is not "&gt;".
the thing is that it doesn't work on all tags for some reason and I
was wondering on a principal base if doing a [^somestring] suppose to
work ?

No, using [^string] wont work as you're expecting. Just like
[string] doesn't match 'string' but only one of the letters s, t, r,
i, n, or g [^stirng] just matches one letter which isn't in 'string'.

What you need is a negative look-ahead (?!string). Read 'perldoc
perlre' for the explanation of it.

//Makholm
 
J

John W. Krahn

ikeon said:
I have a script that I convert xml tags to html. like "<" I convert to
"&lt;" and so on.
after the conversion I need to capture the information inside the tag.
let take for example the string "&lt;abcd&gt;: which is equivalent to
"<abcd>".
I tried to capture the "abcd" which can be different from tag to tag
in the following way:

/\&lt\;([^\&\gt\;]*)/

You probably want something like:

/&lt;(.*?)&gt;/



John
 
I

ikeon

ikeon said:
I have a script that I convert xml tags to html. like "<" I convert to
"&lt;" and so on.
after the conversion I need to capture the information inside the tag.
let take for example the string "&lt;abcd&gt;: which is equivalent to
"<abcd>".
I tried to capture the "abcd" which can be different from tag to tag
in the following way:
/\&lt\;([^\&\gt\;]*)/

You probably want something like:

/&lt;(.*?)&gt;/

John

The (?!string) didn't work for some reason but I have learned a lot
from "perldoc perlre" ;)
The solution was /&lt;(.*?)&gt;/ which is the simple one. I tried it
with only (.*) but it was "greedy".

Thanks John and Peter for your quick respone.
 
S

sln

Hi All,
I have a script that I convert xml tags to html. like "<" I convert to
"&lt;" and so on.
after the conversion I need to capture the information inside the tag.
let take for example the string "&lt;abcd&gt;: which is equivalent to
"<abcd>".
I tried to capture the "abcd" which can be different from tag to tag
in the following way:

/\&lt\;([^\&\gt\;]*)/

like match "&lt;" and then match anything that is not "&gt;".
the thing is that it doesn't work on all tags for some reason and I
was wondering on a principal base if doing a [^somestring] suppose to
work ?

Thanks.

I'm still confused with your terminology 'xml tags to html'.
So be it.

How do you go from "<abcd>" to "&lt;abcd&gt;" without capturing
'abcd' ?


sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top