Click here to get back home

negate a match in regex

 HomeNewsGroups | Search
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
negate a match in regex ikeon 11-27-2008
Posted by ikeon on November 27, 2008, 3:40 am
Please log in for more thread options
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.

Posted by Peter Makholm on November 27, 2008, 3:50 am
Please log in for more thread options

> 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

Posted by John W. Krahn on November 27, 2008, 5:13 am
Please log in for more thread options
ikeon wrote:
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by ikeon on November 27, 2008, 1:30 pm
Please log in for more thread options
> ikeon wrote:
>
> > 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
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0--=
Larry Wall

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.

Posted by sln on November 28, 2008, 1:38 pm
Please log in for more thread options

>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


Similar ThreadsPosted
need to negate regex in middle of expression June 20, 2005, 6:19 am
Multi-Match (to Array) Regex with a precodition match? August 5, 2007, 2:43 pm
RegEx Help, Please? (match after n) June 26, 2005, 10:49 pm
regex to match any url February 14, 2006, 4:02 pm
Printing regex match September 25, 2004, 1:27 pm
regex: match at least one of two expression October 12, 2004, 10:07 am
match regex split January 5, 2005, 9:09 pm
RegEx How to not match a string October 3, 2005, 3:54 pm
"Close match" regex December 18, 2005, 1:55 pm
Regex - Unexpected match for /\r/ February 6, 2007, 8:28 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

Friends:

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap