Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Reverse regexp a regexp?

by rubasov (Friar)
on Feb 13, 2010 at 00:50 UTC ( #822965=note: print w/ replies, xml ) Need Help??


in reply to Reverse regexp a regexp?

I am also not sure what you exactly want to achieve by your second question, however try this:

#! /usr/bin/perl use strict; use warnings; use Regexp::Assemble; my @urls = qw( http://www.youtube.com/user/test1 http://www.youtube.com/user/test2 http://www.youtube.com/user/test3 http://www.youtube.com/user/test4 http://www.youtube.com/user/test5 ); my $r = Regexp::Assemble->new; for (@urls) { $r->add( quotemeta ); } print $r->re, $/; __END__
output: (?-xism:http:\/\/www\.youtube\.com\/user\/test[12345])

And you may also find valuable the String-Approx distribution from CPAN which implements approximate string matching based on Levenshtein distance.

Hope this helps.


Comment on Re: Reverse regexp a regexp?
Select or Download Code
Re^2: Reverse regexp a regexp?
by freakingwildchild (Scribe) on Feb 13, 2010 at 23:09 UTC
    Very interesting approach of assembling regexes; although this would not generate the results required when inputting real life data like:

    my @urls = qw( http://www.youtube.com/user/jantje http://www.youtube.com/user/pieter http://www.youtube.com/user/frida http://youtube.com/user/channel http://youtube.com/channel/doesntexist http://youtube.com/user/blah http://www.youtube.com/user/bianca_sterlings http://www.youtube.com/user/blango );

    Which returns:

    (?-xism:http:\/\/(?:www\.youtube\.com\/user\/(?:b(?:ianca_sterlings|la +ngo)|jantje|pieter|frida)|youtube\.com\/(?:user\/(?:channel|blah)|cha +nnel\/doesntexist)))

    while it should return roughly: ^http:\/\/(?:www\.)youtube\.com\/user\/(.*)$

    What this module offers is a rather customized approach of how to create regexes on custom user data, for validation; while I'd like to create a regex on public data to extract the right set of data out of a list of url's. Thanks for letting me know this this one; it's a nice toy to play with :D

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-12-26 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls