in reply to Match zero times in regex

It does match zero times. In your code, (?:host){0} is matching "host" zero times starting at position 14.

1 2 3 4 012345678901234567890123456789012345678901234567 permit ip host 10.11.1.1 192.168.100.0 0.0.0.255

What about

if ( my ($pairs) = $entry =~ /^ \s* permit \s+ ip \s+ (?: host \s+ \S+ \s+ )? (.*)/x ) { while ( $pairs =~ /(\S+) \s+ (\S+)/xg ) { my ($ip, $mask) = ($1, $2); ... $ip ... $mask ... } }

Update: Fixed problem with solution.

Replies are listed 'Best First'.
Re^2: Match zero times in regex
by Anonymous Monk on Dec 13, 2011 at 09:21 UTC

      Thanks, fixed. I hate having to use $1 and $2, but forgot that it's required here.

      However, the linked node has no bearing on my mistake. The linked node is about the result of list assignment in scalar context, while my mistake was desiring the scalar context behaviour of m//g while calling it in list context.

      PS — Mini-Tutorial: Scalar vs List Assignment Operator is much more comprehensive about the behaviour of assignment based on context.

Re^2: Match zero times in regex
by SomeNetworkGuy (Sexton) on Dec 13, 2011 at 03:39 UTC
    Thanks, I see now why my regex wasn't working the way I wanted it to.