in reply to Re: Re: How can grep search based on text or pattern based on user input?
in thread How can grep search based on text or pattern based on user input?

Removing the slashes is pretty easy. Here's one (non-optimal) attempt:

$input =~ s!^/(.+)/$!$1!;

With that in place, everything you've described so far, except for the $modifiers, will work. You'd either have to escape everything and wrap the whole grep in an eval block, or use some sort of checkbox or if-else block to check for a case insensitive flag.

Perl won't (as far as I know, and I'm pretty sure about this having just tested it) interpolate a variable in the regex flag position.

  • Comment on Re: Re: Re: How can grep search based on text or pattern based on user input?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: How can grep search based on text or pattern based on user input?
by chipmunk (Parson) on Dec 04, 2000 at 23:02 UTC
    However, Perl does provide the (?modifier) syntax, for putting the modifiers inside the regex: /(?i)abc/ So, to make this into a full example:
    @stuff = ('This is Some text.','This is More text.'); $stext = '/some/i'; ($null, $text, $mod) = split(/\//, $stext); if ($mod and $mod !~ /[^ism]/) { # a little sanity checking on the mo +difers $mod = "(?$mod)"; } else { $mod = ''; } foreach (grep /$mod$text/, @stuff) { print $_, "\n"; }
    Adjust the sanity checking on $mod however you like. BTW, Perl allows (?x) as well as (?ism), but I figured you wouldn't need that here.