On 5/13/06, Richard Bagshaw <[EMAIL PROTECTED]> wrote:
I'm very new to perl and I have been trying to solve a problem for a few
days now, I am reading a file that I use to setup my firewall rules on a
Linux box, the file contains many lines, but as an example I will show
just two here :-
iptables -A INPUT -p tcp -s 123.45.678.90 --dport 22 -j ACCEPT
and ...
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
As you can see, the main difference is that line one has the "source ip"
listed, where as line 2 doesn't. I am writing a regex to basically pull
all of the pertinent parts from the lines. The below expression works
perfectly for the first line, but for the 2nd line it does not work. I
know "why" it doesn't work, but cannot find the correct syntax for
alternation to make the 2nd line work.
if (/(INPUT) -p (...) -s ([0-9\.]*) --dport ([0-9]+) -j ([A-Za-z].*)/gi) {
What you want is an optional match using a trailing '?'. Something
like this would work for matching an optional source address:
(?:-s ([0-9\.]*))?
The initial '?:' makes the outer parentheses non-capturing, so they
won't use up one of the match variables. The inner parens will still
capture for you. The trailing '?' says to match zero or one of the
whole thing.
Doug
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>