Gossamer Forum
Home : General : Perl Programming :

Parsing/RegEx Question

Quote Reply
Parsing/RegEx Question
Hi,

I'm trying to parse some log data, and I'm having trouble with my RegEx.

I'm reading an input file as a list:

@lines = (<FILEHANDLE>);

foreach (@lines) {

if (/^\+/) {

# line starts with a "+", take action here

print "$_\n";

}

}

I'm trying to catch lines in the file that begin with "+", and I thought I could escape it with \ to match that pattern, but my diagnostic print call never occurs when it goes through the file. Am I specifying/escaping the + symbol correctly? I know + is used in RegEx frequently... so it's go to be escaped somehow.



Here's my second RegEx problem. I need to read a list of port numbers that are formatted in the same file. Here's some sample data:



o smtp (25/tcp)

o www (80/tcp)

o pop3 (110/tcp)



How can I structure my if statement to catch that format? Each line with a port starts with an "o"... then the service name (www, smtp, etc.). There is always a (, then the port number (1-65535), a /, and then either tcp or udp is specified... and finally a closing ).

if (/^o /) { print "port found"; }

Is that in the right direction? I need to separate the service name (smtp, www, etc.), the port number, and the port protocol into individual variables or into an array. How can this be accomplished easily?



Any help is greatly appreciated... thanks Smile



[x]
Quote Reply
Re: [xpander] Parsing/RegEx Question In reply to
Your first example looks like it should work although you aren't chomping the lines so you'll end up with a blank line between each as you print a newline in your print statement. Make sure @lines actually contais what you think it does.

For your second example try;

Code:
my ($meth, $port, $proto) = $string =~ m|^o\s(\w+)\s\((\d+)/(\w+)\)$|;
Quote Reply
Re: [xpander] Parsing/RegEx Question In reply to
for your first question...try adding a print statement to show the info it should be reading...

something like;

Code:
@lines = (<FILEHANDLE>);

foreach (@lines) {

chomp;

# print out debug info
print "<font color=red>DEBUG: $_</font>";

# line starts with a "+", take action here
if (/^\+/) {
print "$_\n";
}

}

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] Parsing/RegEx Question In reply to
use Data::Dumper;

- wil

Last edited by:

Wil: Feb 15, 2003, 7:48 AM