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

sorry guys this is probably similar to my previous question

but this time my path looks like this

\sbs\v5\strans.cxx@@\main\den\7 (in_12) \sbs\v5\c\ns.cxx@@\main\den\7 (out_11) \sbs\v5\c\ans.h@@\main\den\3 (in_22)
I need to take off the () at the end so the data should look like this
\sbs\v5\strans.cxx@@\main\den\7 \sbs\v5\c\ns.cxx@@\main\den\7 \sbs\v5\c\ans.h@@\main\den\3
I am not seem to be able to reach the () , I am doing somthing like :
$Path =~ tr/(\w)//d; also $Path =~ s/[(\w)]//;
but it is not touching the () at the end of each line

thanks for help on this one

Edit by tye

Replies are listed 'Best First'.
Re: regex
by Aristotle (Chancellor) on Jul 23, 2002 at 14:50 UTC
    The last one is almost correct. What you want is not a character class though, and the parens will have to be escaped (the following also eats any whitespace preceeding the superfluous bit, for good measure): $Path =~ s/\s*\(\w+\)$//; Update: Fixed typo. Thanks for the catch, redsquirrel :)

    Makeshifts last the longest.

      Oops, you escaped the $ instead of the ), it should read:

      $Path =~ s/\s*\(\w+\)$//;

      --Dave

Re: regex
by Speedy (Monk) on Jul 23, 2002 at 15:17 UTC

    Rather than using a regex, if the pattern is always "some-stuff one-or-more-spaces some-more-stuff" you could use a substr - index combination, like:

    $Path = substr($Path, 0, index($Path,' '));
    where the index function returns the position of the first space and substr pulls out the part of the string to the left of that position. Looks more like Visual Basic than Perl, but in the spirit of "more than one way to do it" works for this pattern.

    Speedy tries to live in the moment

Re: regex
by kodo (Hermit) on Jul 23, 2002 at 14:51 UTC
      I'm not sure he really wants an unanchored regex with /g.. it might have some surprising effects down the road. Note that since you're using .*, so long as the variable contains a single line at a time, the /g won't do anything since the regex machine will match the entire string from the first opening to the last closing paren. As a minor point, if you're substituting against $_ you needn't spell out the variable with =~; although you may have chosen to do so for clarity.

      Makeshifts last the longest.

        Yep I know. That's why i wrote "something like". Because it's not meant to be used as it is, just something like to show what the way could be. I think it's better to show people the "way" how they can learn stuff and not give them code, where they don't know what it's doing. Better to give them a "hint" & some docu so they don't ask the same thing again in an hour just with a different content (that's what's happening pretty often these days)...

        giant