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

Hi there,
I have following substring at the beginning of an output

 my $prefixSysDescr      =   "system\.sysDescr\.0 : DISPLAY STRING- (ascii):";

I want to remove it. As it is only one substring to be removed, I do a kind of case structure like this:

for ( $sysDescr ) { /^$prefixSysDescr/ && do { s/^$prefixSysDescr//; last; }; } # end of case

Neither /../ && do { ..}; nor substitue s/..//; matches as expected. Any idea? thx 4 help

Replies are listed 'Best First'.
Re: No matching
by shmem (Chancellor) on Sep 03, 2009 at 08:16 UTC

    The parens spoil your match. See quotemeta.

    my $prefixSysDescr = quotemeta 'system.sysDescr.0 : DISPLAY STRING- +(ascii):'; my $sysDescr = 'system.sysDescr.0 : DISPLAY STRING- (ascii): fub +ar'; $sysDescr =~ s/$prefixSysDescr//; print $sysDescr,$/; __END__ fubar

    Alternatively, do

    $sysDescr =~ s/\Q$prefixSysDescr\E//;

      This is a very valuable hint. I take care for this in future. \Q with \E didn't worked.

        \Q\E is quotemeta, you must have done it wrong
Re: No matching
by tmharish (Friar) on Sep 03, 2009 at 08:21 UTC
    As already pointed out it will help if you gave us the input.

    In general however I find it very helpful if I start building regular expressions in parts.

    So instead of writing it all out maybe you can start with:
    my $prefixSysDescr = "system.sysDescr"; if( $input_var =~ /$prefixSysDescr/ ) { print "Match!\n"; }
    Once you are sure that that part works you can then start adding more of what you want matched.

    That way you get a clearer idea of where your RegEx is failing - Which I believe is the problem here.


      Your advice starting with smaller parts of the string failed too. So I assume your thinking of RegEx failing was right.
      By accident I looked at the 1st line of my script and discoverd a fishy looking shebang (the exclamation mark/point was missing and a strange path). I discovered the path to perl with

      which perl

      After fixing the shebang with exclamation mark/point and the discovered path and implementing "quotemeta" I restarted the script successfuly. Thx for the ideas

Re: No matching
by grizzley (Chaplain) on Sep 03, 2009 at 08:03 UTC
    You don't have to add backslash before dot character. I bet you must add backslash before special characters - parens in $prefixSysDescr.

    Can you add input string example to your post?