Perl weekly challenge 99
Here are solutions to this weeks challenges from the Perl Weekly Challenge.
You can find my full code on Github
Challenge 1
You are given a string $S and a pattern $P.Write a script to check if given pattern validate the entire string. Print 1 if pass otherwise 0.
The patterns can also have the following characters:
- ? - Match any single character.
- * - Match any sequence of characters.
Example 1:
Input: $S = "abcde" $P = "a*e" Output: 1
Solution
This challenge is relatively simple - converting "file name" wildcards into perl regular expressions. the "*" wildcard is the same as ".*" in perl, "?" is the same as "." in perl.
So we replace them in the regex (and remembering we are tied to the ends of the string).
my $regex = '\A' . ( $pattern =~ s{[*]}{.*}r =~ s{[.]}{?}r ).'\Z';
