General comment:

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(); } }

I like computer programming because it's like Legos for the mind.

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

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.