Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks:
I ask for your paitence and help in this simply REGEX matter for I am REGEX impaired, (no this is not homework)
I have the following string:
$string="(afilename123.txt)";
I need to pull afilename123.txt out of $string and place it into a new variable say $newstring.
What REGEX do I need to pull this off with ease?
Your help is greatly appreciated, Thanks!

Replies are listed 'Best First'.
Re: Simple REGEX Help
by ikegami (Patriarch) on Nov 30, 2004 at 19:18 UTC
    # Captures all but the leading and trailing parens. ($file_name) = $string =~ /^\((.*)\)$/;
    # Captures the filename from the middle of the string. # You might have incorrect results if parens are used in more than one + place. ($file_name) = $string =~ /\((.*)\)/;
    # Removes the leading paren and the trailing paren in place. $string =~ s/^\(//; $string =~ s/\)$//;

    By the way, there's no reason to capitalize "regexp".

Re: Simple REGEX Help
by reneeb (Chaplain) on Dec 01, 2004 at 06:04 UTC
    Why not substr()?

    my $newstring = substr($string,1,-1);