http://www.perlmonks.org?node_id=481602


in reply to Re: Parse::RandGen::Regexp
in thread Parse::RandGen::Regexp

Your first suggestions are close to what I need but I don't want to have to escape characters. What other characters would I have to escape beside '\s'? This gets hard to manage.

So I still have the problem, how do I convert a string

(?smi)^STOR\s^\n{100}

OR

/^STOR\s^\n{100}/smi

into a regexp that the P::RG::R wil handle?

Replies are listed 'Best First'.
Re^3: Parse::RandGen::Regexp
by Tanktalus (Canon) on Aug 07, 2005 at 03:42 UTC

    To convert the first string, (?smi)^STOR\s[^\n]{100}, into a compiled regular expression for P::RG::R, just use qr/$string/, assuming $string contained the string as read from the external source (don't forget to chomp it if that's the case!).

    To convert the second string, /^STOR\s[^\n]{100}/smi, again, assuming $string is read from an external source (not perl code - perl DATA is fine), just use eval "qr$string".

    No matter what, if the string is starting out inside perl code, you have to escape the \'s that have special meaning to the regular expression parser but not to perl, such as \s, \w, \S, \W, \d, \D, ... so that they survive to the parser. This is not needed if the string is stored outside of code because then the perl compiler won't see the \'s.

      OK that makes sense to me now. :-) I tried this and got it working. I'm not sure what you mean by "stored outside of code" though. Could you please clarify?

      Thanks,

      Paul

        This can mean anywhere that you need to read it from a filehandle, or get it as a return from some other function. For example, you could have it in an actual file. You could put it in a __DATA__ or __END__ block at the end of your file and read it from the *DATA filehandle. You could retrieve it using LWP from a webserver somewhere, then parse it from that. As long as it isn't contained as text inside your actual perl code where the perl compiler will see it.

        Here's an example:


        #!/usr/bin/perl -w use strict; use Parse::RandGen::Regexp; my $regexp = <DATA>; chomp $regexp; # get rid of the line termination. my $r = Parse::RandGen::Regexp->new(qr/$regexp/); my $string = $r->pick(match=>1, captures=>{}); print("\$string: $string\n"); __END__ (?smi)^STOR\s[^\n]{100}

        Here we're loading it from the __END__ section. For more info on how to use the DATA filehandle, see perldata. Otherwise, you can take the string, put it in a file, and then open/read/close that file as normal.