Re: help about regex matching



Jeff Pang wrote:
> yes,John's code work well too.But I still don't know why this code
> can't run correctly:
>
> next unless /(\S+).*(\d+\.\d+\.\d+\.\d+)/s;
>
> When I run it,I get these results:
>
> 364 2.168.2.20
> 286.4 2.168.2.21
> 264.4 2.168.2.22
> 138 2.168.2.23
> 562.3 2.168.2.24
> 80.7 2.168.2.25
> 355 2.168.2.26
> 266.5 2.168.2.27
> 327 2.168.2.28
>
>
> somethings as '19' have lost.

That is because .* is greedy and will match everything to the end of the
string and then backtrack until (\d+\.\d+\.\d+\.\d+) matches which specifies
that the first \d+ only has to match at least one digit.

Since the two fields start and end with digits you could do this instead:

next unless /([\d.]+)\D+(\d+\.\d+\.\d+\.\d+)/;


John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: help about regex matching
    ... > Jeff Pang wrote: ... >> yes,John's code work well too.But I still don't know why this code ... > that the first \d+ only has to match at least one digit. ... Prev by Date: ...
    (perl.beginners)
  • Re: How to pass pointer as an function argument in C#?
    ... the piece if code i had sent is the code of linked list, will the code work ... without using pointers. ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Playsound Function
    ... > Why won't the following code work? ... If it works on console, but not on glut, that seems strange. ... monitors you will hear a nice "Bzzzbzzzzbzzz";) ... Prev by Date: ...
    (comp.graphics.api.opengl)
  • Richedit control initialisation
    ... Which initialisations are necessary to make this code work? ... LoadLibraryworks for AtlAxCreateDialog() but it does not for the above statement. ... Michael. ... Prev by Date: ...
    (microsoft.public.win32.programmer.ole)
  • Re: validation in windows forms (C#)
    ... it did made my code work the way I ... Regards, ... Abhishek ... Prev by Date: ...
    (microsoft.public.dotnet.framework.windowsforms)