in reply to How regex works in array mode?

There is no such thing as "array mode" in Perl.    I believe you are refering to "list context"?

([0-9]{3}|[0-9]{2}|[0-9]{1}) could be written more simply as [0-9]{1,3} and it will also avoid the unnecessary capturing parentheses.

use strict; use warnings; my @lines = ("ip:192.168.243.1", "ip:192.168.243.2" =~ /(ip:([0-9]{3}| +[0-9]{2}|[0-9]{1})\.([0-9]{3}| +[0-9]{2}|[0-9]{1})\.([0-9]{3}|[0-9]{2}|[0-9]{1})\.([0-9]{3}|[0-9]{2}| +[0-9]{1}))/); print "@lines\n";

It looks like you want something like:

use strict; use warnings; my @lines = ( "ip:192.168.243.1", "ip:192.168.243.2" ); for ( @lines ) { print "$1\n" if /(ip:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 +})/; }

Replies are listed 'Best First'.
Re^2: How regex works in array mode?
by astronogun (Sexton) on Apr 26, 2012 at 10:14 UTC
    Hi,

    Yes that's the output what i want it to be, you got me thanks :)

    My next question is.. is the 2 value on the array stored at $1? What if I want to use the value the "ip:192.168.243.2" how can i stored that at the other string for example $2 or something. Thanks

    Or something like if the items in the array are matched with the regex (like the code that you made. It will stored at $ipline1 = ip:192.168.243.1 and $ipline2 = ip:192.168.243.2 So that I can choose which one to output or print... Is it possible? Thanks