Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Parse::RandGen::Regexp

by ikegami (Pope)
on Aug 06, 2005 at 16:11 UTC ( #481511=note: print w/replies, xml ) Need Help??


in reply to Parse::RandGen::Regexp

How do I convert ...

You just did. qr/^STOR\s[^\n]{100}/smi will do nicely. Use it as if it was a string. In other words, you don't need to change anything else.

By the way, please edit your post, changing <pre>...</pre> to <code>...</code>, to save the janitors from doing it for you.

Replies are listed 'Best First'.
Re^2: Parse::RandGen::Regexp
by paulski (Beadle) on Aug 07, 2005 at 03:21 UTC
    This won't work. If the string comes in as "/test/smi" and I simply place the string inside the qr, it breaks. e.g. I'd get qr//test/smi//;, which doesn't help me. I can get rid of the close //'s easily enough but getting the /smi outside the regexp is harder.

      You mean you start with /test/smi from some outside source? The only way to use that is to eval it. Of course, that means using eval on something from an outside source, which is a big no-no.

      If the user can only use / as the delimiter, then a simple regexp will process the options:

      use strict; use warnings; # For example, get regexp from command line: $regepx = $ARGV[0]; # Look for and remove end slashes and modifiers. $regexp =~ s{^/} {} or die("Bad input\n"); $regexp =~ s{/([msix]*)$}{} or die("Bad input\n"); my $modifiers_on = $1; my $modifiers_off = join '', grep { index($modifiers_on, $_) < 0 } qw( x i s m ); # Add the modifiers: $regexp = "(?${modifiers_on}-${modifiers_off}:${regexp})"; # Compile the regexp and check for errors: $regexp = eval { qr/$regexp/ } or die("Bad regexp: $@\n"); # Now you can use it: print($str =~ $regexp ? 'match' : 'no match', "\n");

      It's trickier if you want to support substitutions and the (g)lobal modifier. You're better off asking for these as seperate arguments. (The search string, the replace string and the modifiers.)

        Thanks, this is really helpful. :-)

        Paul

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2021-05-11 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (119 votes). Check out past polls.

    Notices?