In Perl:
my $string = "This is a test";
say "String matches" if $string =~ /this is a test/; # Doesn't print
say "String sort of matches" if string =~ /this is a test/i; # Prints
Adding the i
flag onto the end of the RE match causes the match to ignore case.
I have a program where I specify the regular expression to match in a separate data file. This works fine. However, I'd like to be able to expand that and be able to specify the regular expression flags to use when checking for a match.
However, in Perl, those RE flags cannot be in a scalar:
my $re_flags = "i";
my $string = "This is a test";
say "This sort of matches" if $string =~ /this is a test/$re_flags;
This results in:
Scalar found where operator expected at ,,, line ,,, near "/this is a test/$re_flags"
(Missing operator before $re_flags?)
syntax error at ... line ..., near "/this is a test/$re_flags"
Execution of ... aborted due to compilation errors.
Is there a way to use RE flags stored in a scalar variable when evaluating a regular expression?
I know I can use eval
:
eval qq(say "This worked!" if \$string =~ /this is a test/$re_flags;);
But I'd like a better way of doing this.