Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Dear Monks, I'm using perl for processing ancient texts (which is cool!), the problem is: I am having troubles with pattern matching in regular expressions. More in detail, I have a list of exemplars of Caesar's "De bello gallico", some of them being partly damaged. The first line of each exemplar is loaded in to an array as follows:
$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";
The broken words are expressed by "...", which means to "one or more words are missing". I would like to compare these strings so that perl outputs a message when they do not match. This is useful to spot unexpected variations. In this case, only the last string should not match the others. My idea is to substitute "..." with ".+", and so far I got this:
for ($i=0;$i<=$#var;$i++) { $var[$i] =~ s/\.\.\./\.\+/g; for ($j=$i+1;$j<=$#var;$j++) { $var[$j] =~ s/\.\.\./\.\+/g; if ($var[$i] !~ m/$var[$j]/) { print "$i-$j:\t[$var[$i]] and [$var[$j]] DO NOT MATCH!\n"; } } }
which prints:

0 - 10 DO NOT MATCH!
1 - 5 DO NOT MATCH!
1 - 6 DO NOT MATCH!
1 - 7 DO NOT MATCH!
1 - 8 DO NOT MATCH!
1 - 9 DO NOT MATCH!
1 - 10 DO NOT MATCH!\
2 - 4 DO NOT MATCH!
2 - 5 DO NOT MATCH!
2 - 6 DO NOT MATCH!
2 - 7 DO NOT MATCH!
2 - 8 DO NOT MATCH!
2 - 9 DO NOT MATCH!
2 - 10 DO NOT MATCH!
3 - 4 DO NOT MATCH!
3 - 5 DO NOT MATCH!
3 - 6 DO NOT MATCH!
3 - 7 DO NOT MATCH!
3 - 8 DO NOT MATCH!
3 - 9 DO NOT MATCH!
3 - 10 DO NOT MATCH!
4 - 5 DO NOT MATCH!
4 - 6 DO NOT MATCH!
4 - 7 DO NOT MATCH!
4 - 8 DO NOT MATCH!
4 - 9 DO NOT MATCH!
4 - 10 DO NOT MATCH!
5 - 6 DO NOT MATCH!
5 - 7 DO NOT MATCH!
5 - 8 DO NOT MATCH!
5 - 10 DO NOT MATCH!
6 - 7 DO NOT MATCH!
6 - 8 DO NOT MATCH!
6 - 10 DO NOT MATCH!
7 - 8 DO NOT MATCH!
7 - 10 DO NOT MATCH!
8 - 10 DO NOT MATCH!
9 - 10 DO NOT MATCH!

Is there a way to put special characters in the left part of the regular expression? I think this is what goes wrong with my idea, but I can't find the solution for it. Thank you so much for your help!

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2014-08-28 05:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (256 votes), past polls