Parsing and regex



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

.



Relevant Pages

  • Re: Parsing and regex
    ... I am writing a program to parse a Cisco ASA log file. ... digit rule number which is unique, a severity level, the full ASA rule ... regex "rule" in the database. ... Also "\s" in a double quoted string is just an 's' character. ...
    (comp.lang.perl.misc)
  • Re: sscanf in c#
    ... |> | With sscanf, I believe you can do something like: ... |> I prefer using TryParse over RegEx, just a matter of taste, and quite ... some minimal code arround the individual type's Parse and TryParse ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sscanf in c#
    ... |> | With sscanf, I believe you can do something like: ... |> I prefer using TryParse over RegEx, just a matter of taste, and quite ... some minimal code arround the individual type's Parse and TryParse ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Mechanize
    ... All you can really do using mechanize is parse the ... > I need to parse out data from the page that is neither a link or form. ... > I need to read the content a line at a time and using a regex find the ... from the POD for WWW::Mechanize: ...
    (perl.beginners)
  • Extracting Name Value Pairs
    ... Hi I have a long line of text that I want to parse for name/value pairs ... can i use RegEx to extract name value pairs? ... A resource for the Microsoft Dynamics Community ...
    (microsoft.public.scripting.vbscript)