Re: numbers and strings and regex?





Geoff Cox wrote:

I have a series of files such as

blue-green-01-01.doc to blue-green-01-09
blue-green-01-11 up to
blue-green-01-32


and am trying to evaluate the second set of 2 digits using File::Find
and this bit of code

if ($name =~ /.*?(\d{2})-((\d{1})(\d{1}))\.jmx/i) {
my $both = $2;
my $first = $3;
my $second = $4;


                          if ($4 <= 9)
                         { $exnum = $4;
                         }
                         else {my $exnum = $both;
                         }

obviously something wrong here - where am I wrong?

Rather hard to know where to start at a guess the central problem is that $4 is a single digit so ( $4 <= 9 ) is always true. You are also capturing subexpressions you don't need.


You you appear to be delaring $esnum in the wrong place.

I also suspect you be anchoring your regex to the end of the string raher than using .* to find the last match.

Perhaps you should try something simple like...

 if ($name =~ /\d{2}-(\d{2})\.jmx$/i)    {
    my $exnum = 0+$1;
 }
.



Relevant Pages

  • Re: numbers and strings and regex?
    ... >I also suspect you be anchoring your regex to the end of the string ... >raher than using .* to find the last match. ...
    (comp.lang.perl.misc)
  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [Slightly OT] Case against ".*" regexp
    ... string and I guess you would say the match is pretty pointless. ... people use .* rather than spending the time to analyse the real patterns ... Anchoring to the beginning/end of the string ... Think about ways to give the regexp engine as many clues or as much ...
    (comp.lang.ruby)