in reply to string substitution within regular expressions?
But when you actually use it, you have /$regex/, so $regex gets interpolated into the pattern you said it was. The dollar signs are end-of-string indicators. What you really want is to eval it.
The usual caveats about knowing that the contents of a string-eval are safe apply.sub findString($) { my $line = shift; print "found\n" if $line =~ eval "qr/$regex/"; }
You can also use the experimental delayed-evaluation feature, though it still requires the variables to be in scope:
my $s = 'Tue Feb 8 11:11:11 2005: blah blah blah'; my ($wday, $mon, $mday, $year); my $regex = qr/^(??{$wday}) (??{$mon})\s+(??{$mday}) \d{2}:\d{2}:\d{2} + (??{$year}):/; $wday = 'Tue'; $mon = 'Feb'; $mday = '8'; $year = '2005'; sub findString; findString($s); sub findString($) { my $line = shift; print "found\n" if $line =~ /$regex/; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: string substitution within regular expressions?
by Anonymous Monk on Feb 09, 2005 at 03:29 UTC | |
by Roy Johnson (Monsignor) on Feb 09, 2005 at 12:16 UTC | |
by Anonymous Monk on Feb 09, 2005 at 18:54 UTC | |
|
Re^2: string substitution within regular expressions?
by Anonymous Monk on Feb 08, 2005 at 23:40 UTC | |
by Roy Johnson (Monsignor) on Feb 08, 2005 at 23:42 UTC |