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

I trying to understand why this code wont remove the extra spaces if it sees more than 2 spaces in the string, instead it is ignoring them, any suggestion?
#!/usr/bin/perl use warnings; use strict; my $string = "“The Class Description_ Test”"; $string =~ s/[^a-zA-Z0-9,_\s{2,}]+//g; print $string; # Expecting: The Class Description_Test
Thanks for looking!

Replies are listed 'Best First'.
Re: Removing unwanted characters and extra spaces from string.
by haukex (Archbishop) on Sep 27, 2018 at 14:29 UTC

    The {2,} on \s won't work inside a []. Try this instead: s/(?:[^a-zA-Z0-9,_\s]+|[\s_]\K\s+)//g (Note: The link needs a fairly modern browser to work.)

    Update: I should mention that this regex also removes the space between Description_ and Test because that's what you showed in your expected output, but hippo is right to ask whether this is what you want. As always with regexes, please provide as many example inputs and outputs as possible, along with a clear spec!

      No, it seems to be doing what I was expecting, if it finds more than two spaces, remove, just had to read more on the \K. flag
      I am working now on, if there are more than 2 spaces between words remove and leave only one space between words.
      Thank you for your help!
Re: Removing unwanted characters and extra spaces from string.
by hippo (Archbishop) on Sep 27, 2018 at 14:41 UTC
    # Expecting: The Class Description_Test

    Your spec suggests that there should be a space between the _ and the T - is this not the case?