in reply to remove chars with regex

The error come from using = when you meant to use =~.

But the pattern is also wrong. There will never be 5 non-space characters before the first character of the string, for starters.

Fixed:

my ( $output ) = $char =~ /(?<=^\S{5})(\S+)(?=\S{5}\z)/;

There's really no point in using look-arounds or \S, though.

my ( $output ) = $char =~ /^.{5}(.*).{5}\z/s;

Faster and cleaner:

my $output = substr( $char, 5, -5 );

Replies are listed 'Best First'.
Re^2: remove chars with regex
by ForgotPasswordAgain (Vicar) on Feb 27, 2025 at 03:07 UTC
    I think that would return the middle part but not result in "(5 chars) ... (5 chars)". More like
    $ perl -Mstrict -wE'my $char = "0x388c818ca8b9251b398a736a67ccb19297"; substr($char, 5, -5, " ... "); say $char' 0x388 ... 19297
    (assuming the string is long enough)

      To remove the middle instead of keeping the middle, you could use

      substr( $char, 5, -5, "" ); # In-place
      or
      my $output = substr( $char, 0, 5 ) . substr( $char, -5 );