Home: Perl Programming Help: Beginner:
regex filter string



orange
User

Dec 29, 2014, 4:57 AM


Views: 1120
regex filter string

is there a simpler way to do this:

Code
 ($string) = ($string =~ /^\((.*?)\)$/);


thanks.


Zhris
Enthusiast

Dec 29, 2014, 3:34 PM


Views: 1071
Re: [orange] regex filter string

Hi,

A substitution would be more appropriate. The non greedy ? is probably not necessary either since you are effectively asking for everything between the first and last chars as per the anchors:


Code
$string =~ s/^\((.*)\)$/$1/;


Another alternative could be to use substr if you are certain $string is always a string wrapped in parenthesis:


Code
substr $string, 0, 1, undef; 
substr $string, -1, 1, undef;


Or if you just want to remove any instances of parenthesis:


Code
$string =~ s/[()]//g;


etc...

Regards,

Chris


(This post was edited by Zhris on Dec 29, 2014, 3:40 PM)