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:
By defining a regular expression which matches a number (integer or floating point) and storing it in $num, I am able to simplify the creation of the longer regular expressions against which the data will be tested. The use of $num, combined with the /x modifier, also makes the regular expressions easier to read and maintain.
Storing the regular expressions in a hash makes it straightforward to identify which pattern was matched whenever a match is successful. This could be useful for debugging.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |