Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular Expression Question

Reply
Thread Tools

Regular Expression Question

 
 
sonet
Guest
Posts: n/a
 
      05-13-2007
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?




 
Reply With Quote
 
 
 
 
numberwhun
Guest
Posts: n/a
 
      05-13-2007
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



 
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
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments