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

I'm processing a small file. At the end of the processing, before printing to the screen.. i'm trying to strip the parantheses from a line in the file. Here's the line.. $url = "(http://www.somewebsite.com)";, I want it to look like this:$url = "http://www.somewebsite.com"; The problem is that i have a couple lines earlier in the script that also have a parantheses.. that need to remain. So i'm trying to come up with a regex match that will find the  "( and  )" in the line and remove the paratheses. I tried this: $text =~ s/\)?//; but it removes all the parantheses in the file. Any Idea's? Lisa.

Replies are listed 'Best First'.
Re: stripping
by Paladin (Vicar) on Jan 04, 2003 at 04:15 UTC
    Didn't you ask almost the exact same question a few days ago? Perhaps if you can say how the suggestions there didn't work?

    Try:

    $text =~ s/\)"/"/g
    to remove the ) before any " in the file.

    Update: fixed regex

      yea.. that did the trick.. without the g.. thanx. $text =~ s/\)"/"/g -Lisa
Re: stripping
by sauoq (Abbot) on Jan 04, 2003 at 09:09 UTC

    I'm not suggesting this approach is best but if the line looks exactly like that (or reasonably close) and none of the other lines do you might try something more specific than the other suggestions you've received. Maybe:

    $text =~ s/(\$url\s*=\s*")\((.*?)\)/$1$2/;
    -sauoq
    "My two cents aren't worth a dime.";
    
      Or how about just:

      $text =~ s/"\((.*?)\)"/"$1"/;

      Or even:

      $text =~ s/(?<")\((.*?)\)(?=")/$1/;

      Okay, so you probably don't really want to use the second one.

      Queue
      slowly working his way through Mastering Regular Expressions
        Or how about just:

        According to the original post, there were "a couple lines earlier in the script that also have a parantheses." What if one of them looked like this

        my @parens = ("(", ")");
        for instance?

        It's best to say exactly what you really mean with regular expressions.

        -sauoq
        "My two cents aren't worth a dime.";