in reply to Re: Regex substitute matches second match first?
in thread Regex substitute matches second match first?

Heh, blimey, thank you. So /g sometimes isn't needed - I have read about 'greedy' regex before but never encountered it.

Many thanks for your wisdom!

Nick

  • Comment on Re^2: Regex substitute matches second match first?

Replies are listed 'Best First'.
Re^3: Regex substitute matches second match first?
by stevieb (Canon) on May 25, 2016 at 17:24 UTC

    The /g modifier is for global matches. For instance, if you had numerous IPs on a single line, and you wanted to grab them all. eg: (untested)

    my $line = "1.1.1.1 blah 2.2.2.2"; my @ips = $line =~ /\d+\.\d+\.\d+\.\d+/g;

    Now $ips[0] would be '1.1.1.1' and $ips[1] would contain '2.2.2.2'.

    A couple of docs you can review are perlretut and perlre.

      Thanks for your help, will read up

      Nick