About regular expression question

Discussion in 'Perl Misc' started by sonet, May 13, 2007.

  1. sonet

    sonet Guest

    The $str is the xml string.
    <a>
    <c>123</c>
    <d>abc</d>
    </a> <b attr='456'/> ...

    I need to detect some specific close tag.
    ex:
    (</a> or <a/>) OR (</b> or <b/>)

    1.
    if ($str=~/<\/(a|b)>|<(a|b)\/>/s){
    #do something A;
    }

    2.
    if ($str=~/<\/(a|b)>/s){
    #do something A;
    }elsif($str=~/<(a|b)\/>/s){
    #do something A;
    }

    3.
    if ($str=~/<\/(.*?)>|<(.*?)\/>/s){
    if ($1 eq 'a' || $1 eq 'b'){
    #do something A;
    }
    }

    ===========================================
    Which one is the better? And have any more good RE
    pattern for above example?
     
    sonet, May 13, 2007
    #1
    1. Advertising

  2. sonet

    Klaus Guest

    On May 13, 8:35 am, "sonet" <> wrote:
    > The $str is the xml string.
    > <a>
    > <c>123</c>
    > <d>abc</d>
    > </a> <b attr='456'/> ...
    >
    > I need to detect some specific close tag.
    > ex:
    > (</a> or <a/>) OR (</b> or <b/>)


    [ snip ]

    > And have any more good RE
    > pattern for above example?



    see Perlfaq4: How do I find matching/nesting anything ?
    >> ================================
    >> This isn't something that can be done in one regular
    >> expression, no matter how complicated.
    >> ================================


    I would suggest to employ one of the XML modules on CPAN:
    http://search.cpan.org/search?query=XML&mode=all

    --
    Klaus
     
    Klaus, May 13, 2007
    #2
    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. Andrew Munn

    Regular expression question...

    Andrew Munn, Jun 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    2,138
    rakesh sharma
    Jun 30, 2003
  2. Glenn Kidd

    Regular expression question

    Glenn Kidd, Aug 18, 2003, in forum: Perl
    Replies:
    0
    Views:
    933
    Glenn Kidd
    Aug 18, 2003
  3. VSK
    Replies:
    2
    Views:
    2,311
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    852
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,978
    vforvikash
    Dec 29, 2008
Loading...

Share This Page