September 2017
Beginner
402 pages
9h 52m
English
Let us modify the phone number regex so that it forces the regex to match with the whole string containing a potential phone number:
/ ^ \+? <[\d\s\-]>+ $ /;
Here, ^ is the anchor that matches at the beginning of the string and does not consume any characters. On the other side of the regex, $ requires that the end of the regex matches the end of the string. So, a valid phone number, say +49 20 102-14-25 will pass the filter, while a mathematical expression such as 124 + 35 - 36 will not.
For better visibility, anchors can be written on separate lines in the code:
my $rx = / ^ \+? <[\d\s\-]>+ $/;say 'OK' if '+49 20 102-14-25' ~~ $rx; # OKsay 'Not OK' if '124 + 35 - 36' !~~ $rx; # Not ...