In some cases, don't we want to use c as well as g so as to advance our pos() within the string regardless of if we match?
Re: $':I have read that using the regexp variables in that family is really inefficient and likely best avoided when possible.
Code:Assuming a) that there are spaces separating all groups of consecutive digits or letters and b) each group consists only of either digits or letters and c) (crucial for my implementation of the 'more complex' problem), digit groups are never consecutive (meaning, you'll never see a pattern like QKLJB 9234 KJLH 324 9874 in the data). Alternately, if there are consecutive integers, you only care about the most recent letter group (even if it was five groups ago). Also, you only care about the last group of letters before each integer :)
#!blah #untested! #use strict, etc. my $bs; #your big string--how you get the data in there is up 2 u my ($lms, $lmn); #last matched string,num my $max = 0;# biggest while ($bs !~ m/\G\z/ms){ if ($bs =~ m/\G([[:upper:]]+)\s*/g){#[:alpha:],whatever $lms = $1; }elsif ($bs =~ m/\G(\d+)\s*/g){#[:alpha:],whatever #up to the closing brace before else, #$lms corresponds to the text before this number $lmn = $1; if ($lmn > $max){ #at this point, $lms is the text before the biggest int so far $max = $lmn; } }else{ die "unexpected data near character".pos(); } }
In reply to Re: Help with regex, how to get the largest integer in a string?
by OfficeLinebacker
in thread Help with regex, how to get the largest integer in a string?
by coltman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |