Parsing and regex
- From: "toof57" <d_toth@xxxxxxx>
- Date: 19 Oct 2006 18:45:44 -0700
Hi,
I am writing a program to parse a Cisco ASA log file. I've broken the
line into date info, IP of recording device, ASA rule # and the
"payload" or rule data. I have created a database that contains the 6
digit rule number which is unique, a severity level, the full ASA rule
(e.g %ASA-5-302020), a short description to print on the summary
report, and I have parsed each rule using regex and plan on storing the
regex "rule" in the database. The first thing I do is load the Rule
database into a hash with the 6 digit rule being the key. The hash
looks like
$ASARule{$Rule}->{'ASASev'}
$ASARule{$Rule}->{'ASADescription'}
$ASARule{$Rule}->{'ASAParse'} etc.
My question is:
Can you assign a regex to a variable and then use it to parse a string
variable into the corresponding values (which I also plan to store in a
variable). Here's a sample:
$String="<some string>";
$Parse="/regex/";
@Variables="$var1 $var2 . . . $varx";
@Variables = $String =~ $Parse;
I'm trying to parse the string with the assigned regex expression and
place the values in corresponding variables.
My first problem is:
I tried this code
$String="take me out 2 the ballgame";
$Parse1="/^take me ([a-zA-Z0-9]+)\s(.*)/";
# <- I assign the regex to the statement
($v1, $v2) = $String =~ $Parse1;
print "$String =~ $Parse1\n";
print "$1 $2\n";
print "v1= $v1, v2 = $v2\n";
output is
take me out 2 the ballgame =~ /take me ([a-zA-Z0-9]+)\s(.*)/
v1 = , v2=
So it appears that the command is correct but it is not parsing into
the variables.
if I change the code to
$String="tak me out 2 the ballgame";
$Parse1="/^take me ([a-zA-Z0-9]+)\s(.*)/";
#<- I "hard code" the regex in the statement
($v1, $v2) = $String =~ /^take me ([a-zA-Z0-9]+)\s(.*)/;
print "$String =~ $Parse1\n";
print "$1 $2\n";
print "v1= $v1, v2 = $v2\n";
output is
take me out 2 the ballgame =~ /take me ([a-zA-Z0-9]+)\s(.*)/
out 2 the ballgame
v1 = out, v2= 2 the ballgame
It appears that putting the regex in the assignment statement works
whereas assigning the regex using the variable doesn't. Am I missing
something or will this work or not?
I also tried using #{Parse1} in the assigment and that didn't work
either.
If I can assign the regex and have it parse into the variables, all
will be good. If not I have to rethink how to parse the payload. But
I'd like to do it this way so I can just use the hash loaded with all
the info I need to break the data in the rule into variables and then
store it on a database. I'm trying to eliminate a rather large and
combersome swirch statement to "hard code" the rule regex for every
possibility. Any help or takers???
David
.
- Follow-Ups:
- Re: Parsing and regex
- From: Ferry Bolhar
- Re: Parsing and regex
- From: John W. Krahn
- Re: Parsing and regex
- From: John Bokma
- Re: Parsing and regex
- Prev by Date: Re: unable to calculate large file size
- Next by Date: Re: pasting files end to end
- Previous by thread: pasting files end to end
- Next by thread: Re: Parsing and regex
- Index(es):
Relevant Pages
|