Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Word Comparison

by hipowls (Curate)
on Mar 27, 2008 at 05:01 UTC ( #676633=note: print w/replies, xml ) Need Help??

in reply to Word Comparison

Now a regex variant;) I don't know if you'd want to use it but it does demonstrate building a regex at run time.

while ( my $line = <DATA> ) { my ($new, $old) = split /\s+/, $line; my $rx = regex($new); print "$new can ", ( $old =~ /$rx/? '': 'NOT ' ), "be made from $o +ld\n"; } sub regex { my %letters; $letters{$_}++ for split //, lc shift; my $regex = join '', map { "(?=(?:.*${_}){$letters{$_}})" } keys % +letters; return qr/\A$regex/si; } __DATA__ dog good food fodder lot total fuse useful poor porridge root rotor __END__ dog can be made from good food can NOT be made from fodder lot can be made from total fuse can be made from useful poor can NOT be made from porridge root can be made from rotor
I'll show the regular expression generated from 'root' in detail
my $regex = qr{ \A # anchor at start for efficiency (?= # positive lookahead for (?: .* # anything r # with an 'r' after it ){1} # at least once ) (?= # positive lookahead for (?:.*t){1} # a 't' ) (?= # positive lookahead for (?:.*o){2} # for two 'o's ) }six; # ignore case
All the lookaheads have to succeed for the regex to match.

On a side note I benchmarked the variants (?:.*r), (?:[^r]*+r) and (?:[^r*]r) on different input strings and found the first was usually fastest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://676633]
[Corion]: Mr. Muskrat: I'm not sure if it really happens more often, but I don't exactly know either
[LanX]: yep
[LanX]: more often for some weeks now
[Corion]: I think I'll have to manually (as god) intervene with that node, as the simple reparenting didn't seem to fix the parent/child relationship of the nodes
[Corion]: I think I have an idea but I'll have to open a ticket with on that - hopefully I get to that on the weekend
LanX imagines a burning thorn bush
[Mr. Muskrat]: Thank you!
[Mr. Muskrat]: Oh that is odd. I got the message that it was reparented but yeah, it didn't actually do it. lol
[Corion]: LanX: No, Co-Rion appears as Monkey God :-)
[Corion]: Mr. Muskrat: Yeah, that message is automatic when the "normal" reparenting is done, but I think some rows that make up the node were not added to the DB properly

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-01-19 16:26 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.