Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Reverse engineering regular expressions

by onegative (Scribe)
on Aug 01, 2005 at 17:48 UTC ( [id://479991]=note: print w/replies, xml ) Need Help??


in reply to Reverse engineering regular expressions

I believe many have touched on the fact that infinite possibilities exist for any given situation unless you already had some notion of what you expected in the first place. I use the following tool to help with tricky regex expressions, not because I can't do them myself, but rather to help speed up the process. http://weitz.de/regex-coach/

Replies are listed 'Best First'.
Re^2: Reverse engineering regular expressions
by paulski (Beadle) on Aug 06, 2005 at 10:34 UTC
    I found a better module on CPAN that does exactly what I need Parse::RandGen::Regexp.pm. Thanks for all the input guys.

    The problem I'm having now is how to get a regexp into this function. I wrote a stub progam to test.

    #!/usr/bin/perl -w
    
    use strict;
    use Parse::RandGen::Regexp;
    
    my $regexp = "/^STOR\s^\n{100}/smi";
    my $r = Parse::RandGen::Regexp->new($regexp);
    my $string = $r->pick(match=>1, captures=>{}); 
    print("\$string: $string\n");
    
    This throws the following error.
    Unrecognized escape \s passed through at ./regexp2.pl line 6.
    %Error:  Parse::RandGen::Regexp has an element that is not a Regexp reference (ref="")! at /usr/lib/perl5/site_perl/5.8.6/Parse/RandGen/Regexp.pm line 36
    	Parse::RandGen::Regexp::_newDerived('Parse::RandGen::Regexp=HASH(0x9dcdd88)', 'HASH(0x9e5f138)') called at /usr/lib/perl5/site_perl/5.8.6/Parse/RandGen/Condition.pm line 81
    	Parse::RandGen::Condition::new('Parse::RandGen::Regexp', '/^STORs^\x{a}{100}/smi') called at ./regexp2.pl line 7
    
    Now I need the string in regexp format to pass to the function. I could just put the string in qr//s but in my real program I need to read the regexps from a list so they will come in scalar format.

    i.e. How do I convert:

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

    to

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

    I'm not sure how to do this conversion.

    Thanks,

    P.

      This is a duplicate of my answer to Parse::RandGen::Regexp

      As the qr// method runs in interpolative context one can do

      use strict; my $match='test'; my $regex_match=qr/$match/i; my $test_value='This is a Test'; print 'It matches' if $test_value=~m/$regex_match/;

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found