September 2017
Beginner
402 pages
9h 52m
English
We already have seen many times that additional spaces are ignored in regexes, and they are often used to make a regex more readable. In some cases, however, especially when a regex should match with a string with spaces, it is better to disable this feature and demand that spaces match literary.
In the following example, we extract the three parts from the date—day, month, and year. As there are spaces in the original human-oriented string, we need to take care of them in the regex. By default, spaces are ignored and the regex should include \s in the places where a space is expected:
my $date = '19 April 2017';$date ~~ / (\d+) \s (\w+) \s (\d+) /;say "Year = $2, month = $1, day = $0";
With the :s adverb, literal spaces inside ...
Read now
Unlock full access