Hello merrymonk,

My approach is similar to those already given:

#! perl use strict; use warnings; my $num = qr{ \d+ (?: \. \d* )? }x; my @patterns = ( qr{ ^ N $num G03X $num Y $num K $num J $num H1M25 $ }x, qr{ ^ N $num M20 $ }x, qr{ ^ N $num G45 $ }x, qr{ ^ N $num G01X $num Y $num C $num M25 $ }x, ); my $count = 1; my %patterns = map { $_ => $count++ } @patterns; OUTER: while (my $line = <DATA>) { chomp $line; for (keys %patterns) { if ($line =~ $_) { print "$line matches pattern $patterns{$_}\n"; next OUTER; } } print "No match found for $line\n"; } __DATA__ N335G03X247.16Y580.07K6.89J96.62H1M25 N340M20 N345G45 N350G01X832.56Y692.92C275.44M25 N355M20 N360G46C0 N365G03Y720.88I146.74J13.98H36M25

Output:

19:53 >perl 1287_SoPW.pl N335G03X247.16Y580.07K6.89J96.62H1M25 matches pattern 1 N340M20 matches pattern 2 N345G45 matches pattern 3 N350G01X832.56Y692.92C275.44M25 matches pattern 4 N355M20 matches pattern 2 No match found for N360G46C0 No match found for N365G03Y720.88I146.74J13.98H36M25 20:04 >

Notes:

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Using regular expressions to find patterns in machine tool data by Athanasius
in thread Using regular expressions to find patterns in machine tool data by merrymonk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.