With a regular expression, match letters and numbers but not whitespaces

201 views Asked by At

This is a beginner's question but I'm just getting started and need some help.

I have this string:

dog and cat

Using PCRE-Regexes, I want to match this without the whitespaces and capture it as a group, capturingdogandcatas $1.

I got this far (\w*). Putting Parentheses outside for the capture, then read any letter, digit, or underscore. This returns dog. See it here: https://regex101.com/r/pL1bY1/3

Now I'd like to skip the adjcent whitespace and continue with and. How do I do that?

1

There are 1 answers

3
fugu On

In perl you can just substitute:

my $string = 'dog and cat';

$string =~ s/ //g;

print "$string\n";