Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Is there any way to put regex modifiers into a variable?

by davies (Parson)
on Mar 11, 2018 at 19:06 UTC ( #1210678=note: print w/replies, xml ) Need Help??


in reply to Is there any way to put regex modifiers into a variable?

You can do it slightly differently by putting the regex modifier(s) inside the regex:

use strict; use warnings; my $opts = "io"; my $regex = "test"; while (<DATA>) { my $line = $_; print $line if $line =~ /(?$opts)\Q$regex\E/; } __DATA__ untested unknown

Note that this reveals a slight problem - I can't find your 'o' modifier in the docs. Note also that I have wrapped the regex itself in \Q\E to prevent problems if it contains metacharacters.

Regards,

John Davies

Replies are listed 'Best First'.
Re^2: Is there any way to put regex modifiers into a variable?
by hilitai (Monk) on Mar 11, 2018 at 20:23 UTC

    Yes! Thank you! That is the slap upside the head that I needed.

    With regards to the /o modifier, see the Perlop man page, Regexp Quote-Like Operators.

      the /o modifier

      Although I'm having trouble finding it at the moment, I think there was some recent discussion that /o is often not what you want. All I found so far is /o is dead, long live qr//!, although that's not recent. There's the FAQ What is /o really for?. You could perhaps look into qr//.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210678]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2018-10-15 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (82 votes). Check out past polls.

    Notices?