use strict;
my $str = 'K\'8134567
K\'81345678
K\'6134516789
K\'61345678
K\'643456
K\'646345678
K\'1234567890
K\'12345678901
K\'1454567890 <<<--- want 145 returned and not 1
K\'13345678901 <<<--- want 133 returned and not 1
K\'3214567890123
K\'32134567890123
K\'3654567890123
K\'8934567890123
K\'6554567890123
I am interested in the digits after K\'
I am looking to do this using regex but not sure if it can be done. What I want is:
if the number starts with 81 return 81
if the number starts with 61 return 61
...
if the number starts with something i am not interested in return other(or its first digits of 1-3)
The above criteria works:
but what I also want is:
if the fist digit is 1 then return 1 BUT
if the fist digit is 1 and the 2nd and 3rd digit are 45 return 145 and don\'t return just 1
if the fist digit is 1 and the 2nd and 3rd digit are 33 return 133 and don\'t return just 1
I presume I have to put something inside this part of the regex |(1)\\d+|
Questions:
Does regex sort the data first?
Is the order of the regex search important to how it is implemented? i deally I do not want this.
';
my $regex = qr/K'(?P<name1>81|61|64|44|86|678|41|49|33|685|1(?:33|45)?|\d{2,3})\d+/p;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html