negate a match in regex

Discussion in 'Perl Misc' started by ikeon, Nov 27, 2008.

  1. ikeon

    ikeon Guest

    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.
     
    ikeon, Nov 27, 2008
    #1
    1. Advertising

  2. 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
     
    Peter Makholm, Nov 27, 2008
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Nov 27, 2008
    #3
  4. ikeon

    ikeon Guest

    On Nov 27, 12:13 pm, "John W. Krahn" <> 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.
     
    ikeon, Nov 27, 2008
    #4
  5. ikeon

    Guest

    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
     
    , Nov 28, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    640
  2. Rob Mitchell

    [Q] fast way to negate?

    Rob Mitchell, Dec 13, 2005, in forum: Java
    Replies:
    0
    Views:
    363
    Rob Mitchell
    Dec 13, 2005
  3. Replies:
    3
    Views:
    557
    Paul McGuire
    Jan 9, 2005
  4. negate INT_MIN

    , Feb 10, 2007, in forum: C Programming
    Replies:
    2
    Views:
    412
    ¬a\\/b
    Feb 10, 2007
  5. Sherm Pendley

    need to negate regex in middle of expression

    Sherm Pendley, Jun 20, 2005, in forum: Perl Misc
    Replies:
    8
    Views:
    157
    Tad McClellan
    Jun 20, 2005
Loading...

Share This Page