in reply to Using regular expressions to find patterns in machine tool data

Well, if you have N335G03X247.7.16Y580.07K and your pattern is N<n>G03X<x>Y Then we know that numbers are actual numbers 0-9 and can contain a dot as decimal separator, thus the regular expression to match this is:

use strict; use warnings; my $line = "N335G03X247.16Y580.07K6.89J96.62H1M25 N340M20 N345G45"; my($n, $x); if($line =~ /N([0-9\.]+)/){ $n = $1 }; if($line =~ /G03X([0-9\.]+)/){ $x = $1 };
You can then use this to get the rest of the variables, then check if they have the right values... or at least defined, for example if $n and $x has been defined:
if($n && $x){ print "n=$n and x=$x \n" }
You can exchange 0-9 with \d, as in:
if( $line =~ /N([\d\.]+)/ ){ $n = $1 }; if( $line =~ /G03X([\d\.]+)/ ){ $x = $1 };

Replies are listed 'Best First'.
Re^2: Using regular expressions to find patterns in machine tool data
by GrandFather (Saint) on Jun 27, 2015 at 09:11 UTC

    you don't need to quote . in a character set match.

    Perl is the programming world's equivalent of English
Re^2: Using regular expressions to find patterns in machine tool data
by AnomalousMonk (Archbishop) on Jun 27, 2015 at 16:07 UTC

    Further to GrandFather's reply: Also note that the pattern  [\d.]+ will match  '.1.2.3.4.' or  '1....2' or  '......' which may not be what you want. In defining regexes, specificity is a great virtue. Dealing with regular expressions is like dealing with the Djinn from the bottle: they'll both give you exactly what you ask for, so it's best to ask for exactly what you want!


    Give a man a fish:  <%-(-(-(-<