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

    numberwhun Guest

    Sonet,


    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/>)
    >
    > 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?
    >


    Personally, one of the things that I love about perl is that TMTOWTDI.
    Granted, there are ways of doing things that are definitely a lot more
    efficient than others, but with the above examples, I would just pick
    whichever one works for you, as it is a simple enough match. If you are
    truely looking for an opinion, I like #3.

    Regards,

    Jeff
     
    numberwhun, 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,150
    rakesh sharma
    Jun 30, 2003
  2. Glenn Kidd

    Regular expression question

    Glenn Kidd, Aug 18, 2003, in forum: Perl
    Replies:
    0
    Views:
    935
    Glenn Kidd
    Aug 18, 2003
  3. VSK
    Replies:
    2
    Views:
    2,332
  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:
    860
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,997
    vforvikash
    Dec 29, 2008
Loading...

Share This Page