Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > negate a match in regex

Reply
Thread Tools

negate a match in regex

 
 
ikeon
Guest
Posts: n/a
 
      11-27-2008
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.
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      11-27-2008
ikeon <> writes:

> 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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-27-2008
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
 
Reply With Quote
 
ikeon
Guest
Posts: n/a
 
      11-27-2008
On Nov 27, 12:13*pm, "John W. Krahn" <some...@example.com> wrote:
> 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


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.
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      11-28-2008
On Thu, 27 Nov 2008 00:40:00 -0800 (PST), ikeon <> wrote:

>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
[Q] fast way to negate? Rob Mitchell Java 0 12-13-2005 02:53 AM
need to negate regex in middle of expression ebillionaire@gmail.com Perl Misc 8 06-20-2005 04:43 PM
pyparsing: how to negate a grammar knguyen@megisto.com Python 3 01-09-2005 11:38 PM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57