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

Re: Using special characters in left part of a regex match?

by punch_card_don (Curate)
on Feb 05, 2013 at 23:28 UTC ( #1017302=note: print w/ replies, xml ) Need Help??


in reply to Using special characters in left part of a regex match?

To my mind, you have a logic issue long before you have a regex syntax issue.

Does the first phrase ($var[0]) always define the full version of the phrase? If so, ok, straight forward enough. But if not, then how do you decide which phrases define what is "normal" to be in the phrase, and which have a variant? The human brain may be able to see that naturally enough, but your code has no element of that.

If the first phrase always defines the complete phrase, then, easy:

#!/usr/bin/perl -w use strict; use warnings; print "Content-type:text/html\n\n"; my @var; $var[0] = "Gallia est omnis divisa in partes tres"; $var[1] = "Gallia est omnis divisa in ..."; $var[2] = "Gallia est omnis ..."; $var[3] = "Gallia"; $var[4] = "... omnis divisa in ..."; $var[5] = "Gallia est ... tres"; $var[6] = "Gallia ... partes tres"; $var[7] = "Gallia est ... partes tres"; $var[8] = "Gallia ... divisa ... tres"; $var[9] = "... tres"; $var[10] = "quattuor"; my @base_phrase_words = split(/\s/, $var[0]); push(@base_phrase_words, "..."); my %words = map { $_ => 1 } @base_phrase_words; for (my $i=1; $i<=$#var; $i++) { my @partial_phrase_words = split(/\s/, $var[$i]); foreach my $element (@partial_phrase_words){ if (exists($words{$element})) { # do whatever } else { print "<p>Found word $element in phrase $i varies from bas +e phrase\n"; } } }
Output
Found word quattuor in phrase 10 varies from base phrase



Time flies like an arrow. Fruit flies like a banana.


Comment on Re: Using special characters in left part of a regex match?
Select or Download Code
Re^2: Using special characters in left part of a regex match?
by shamat (Novice) on Feb 06, 2013 at 20:44 UTC
    Thank you for this one! Indeed there might not be a "full version", although this case is rare. In addition, the most complete version may not occur as the first element of the array. Sorry if my example was misleading.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-11-28 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (193 votes), past polls