How to add a modifier to a quoted regular (qr) expression
Asked Answered
S

3

6

Is there an easy way to add regex modifiers such as 'i' to a quoted regular expression? For example:

$pat = qr/F(o+)B(a+)r/;
$newpat = $pat . 'i'; # This doesn't work

The only way I can think of is to print "$pat\n" and get back (?-xism:F(o+)B(a+)r) and try to remove the 'i' in ?-xism: with a substitution

Snag answered 10/11, 2011 at 16:15 Comment(0)
Y
6

You cannot put the flag inside the result of qr that you already have, because it’s protected. Instead, use this:

$pat = qr/F(o+)B(a+)r/i;
Yeryerevan answered 10/11, 2011 at 16:32 Comment(0)
U
2

You can modify an existing regex as if it was a string as long as you recompile it afterwards

  my $pat = qr/F(o+)B(a+)r/;
  print $pat, "\n";
  print 'FOOBAR' =~ $pat ? "match\n" : "mismatch\n";

  $pat =~ s/i//;
  $pat = qr/(?i)$pat/;
  print $pat, "\n";
  print 'FOOBAR' =~ $pat ? "match\n" : "mismatch\n";

OUTPUT

  (?-xism:F(o+)B(a+)r)
  mismatch
  (?-xism:(?i)(?-xsm:F(o+)B(a+)r))
  match
Ursola answered 11/11, 2011 at 11:29 Comment(2)
+1 for showing the appropriate way to plant a modifier into an existing regex. The (?…) part is documented in perldoc.perl.org/perlre.html#Extended-PatternsSse
This doesn't work after Perl 5.12 because the regex stringification changed.Outofdate
F
1

Looks like the only way is to stringify the RE, replace (-i) with (i-) and re-quote it back:

my $pat = qr/F(o+)B(a+)r/;
my $str = "$pat";
$str =~ s/(?<!\\)(\(\?\w*)-([^i:]*)i([^i:]*):/$1i-$2$3:/g;
$pati = qr/$str/; 

UPDATE: perl 5.14 quotes regexps in a different way, so my sample should probably look like

my $pat = qr/F(o+)B(a+)r/;
my $str = "$pat";
$str =~ s/(?<!\\)\(\?\^/(?^i/g;
$pati = qr/$str/;

But I don't have perl 5.14 at hand and can't test it.

UPD2: I also failed to check for escaped opening parenthesis.

Fermentation answered 11/11, 2011 at 11:32 Comment(2)
This won't work anymore because the regex stringification doesn't do -options in Perl 5.14.Outofdate
@briandfoy: Thanks for poining out. I've updated my answer, but I'm not sure that the 5.14 part works.Fermentation

© 2022 - 2024 — McMap. All rights reserved.