Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by kennethk (Monsignor)
on Feb 06, 2013 at 00:01 UTC ( #1017304=note: print w/ replies, xml ) Need Help??


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

So, I'm assuming 3 should actually be Gallia ... since it would otherwise be inconsistent with all other options and would not map to the first string. Without the ability to anchor at the front and back, this problem seems unsolvable to me.

If you have a full string, as in case 0, you can trivially use regular expressions. Otherwise, only your leading and trailing words are actually constraining. This strikes me more as a case where you would split your strings on ... and then compare substrings -- this is not consistent with using regular expressions. My version, with index to do the work fragment checking:

#!/usr/bin/perl use strict; use warnings; 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"; for my $i (0 .. $#var) { for my $j ($i+1 .. $#var) { print "$i - $j DO NOT MATCH!\n" unless compare($var[$i], $var[ +$j]); } } sub compare { my @str1 = split /\Q...\E/, shift, -1; my @str2 = split /\Q...\E/, shift, -1; if (@str1 == 1) { # Regex is possible local $" = ".+"; return $str1[0] =~ /^@str2$/; } elsif (@str2 == 1) { # Regex is still possible local $" = ".+"; return $str2[0] =~ /^@str1$/; } else { # Fragment matching # Openings must be consistent if (length $str1[0] > length $str2[0]) { return if index($str1[0], $str2[0]) != 0; } else { return if index($str2[0], $str1[0]) != 0; } # Closings must be consistent, start search from end if (length $str1[-1] > length $str2[-1]) { return if index(reverse($str1[-1]), reverse($str2[-1])) != + 0; } else { return if index(reverse($str2[-1]), reverse($str1[-1])) != + 0; } } return 1; }
which outputs
0 - 10 DO NOT MATCH! 1 - 10 DO NOT MATCH! 2 - 10 DO NOT MATCH! 3 - 10 DO NOT MATCH! 4 - 10 DO NOT MATCH! 5 - 10 DO NOT MATCH! 6 - 10 DO NOT MATCH! 7 - 10 DO NOT MATCH! 8 - 10 DO NOT MATCH! 9 - 10 DO NOT MATCH!

If instead you meant case 10 to be ... quattuor ..., you get

0 - 10 DO NOT MATCH!

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


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:53 UTC
    <quote>So, I'm assuming 3 should actually be Gallia ...</quote> This is correct, thanks for spotting it out. Unfortunately, I might not have a full string in case 0, so I should really compare every variant with anyone else. I also realized that anchor points are important, but case 10 is indeed "... quattuor", and should be evaluated as the only non-variant in the set.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-08-22 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (160 votes), past polls