Can I suggest that you use the perl debugger to help you write your regular expressions, as that way you can very quickly try out different approaches until you have found a regular expression that works.
Unlike graphical debuggers you may be used to from other programming environments, the perl debugger is a command line tool. You may find it unfamiliar, but it has the advantage that you get a perl shell that allows you try out things as much as you like without stepping of the current line.
You can either start your script in the debugger, and then stopping it at the point where the string you are trying to match is loaded into a variable, or you start the debugger without a script using the one liner:
perl -d -e 0
(This works because the -e argument says to run the script on the command line, and the zero is the shortest possible perl script.)
Once you have the debug prompt, you can put your string into a varable, and try out regular exprssons on it. eg:
perl -d -e 0
Loading DB routines from perl5db.pl version 1.32
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(-e:1): 0
DB<1> $message = q(some_fields msg="http_decoder: HTTP.Unknown.Tunne
+lling" some_fields)
DB<2> x $message
0 'some_fields msg="http_decoder: HTTP.Unknown.Tunnelling" some_field
+s'
DB<3> x $message =~ m/msg=\"+((?:([^:,]+):\s|)([^,]+?)\s*(?:\s*,.*?|
+)))\"+/
Unmatched ) in regex; marked by <-- HERE in m/msg=\"+((?:([^:,]+):\s|)
+([^,]+?)\s*(?:\s*,.*?|))) <-- HERE \"+/ at (eval 7)[/usr/share/perl/5
+.10/perl5db.pl:638] line 2.
at (eval 7)[/usr/share/perl/5.10/perl5db.pl:638] line 2
eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $^D =
+ $^D | $DB::db_stop;
$message =~ m/msg=\\"+((?:([^:,]+):\\s|)([^,]+?)\\s*(?:\\s*,.*?|)))\
+\"+/;
;' called at /usr/share/perl/5.10/perl5db.pl line 638
DB::eval called at /usr/share/perl/5.10/perl5db.pl line 3444
DB::DB called at -e line 1
DB<4>
When I tried your regular expression it did not work and perl gave me the error message above. (It looks like your brackets don't match up).
At this point, if it where me, I would start by writing a simpler regular expression, that only captures one element that I am interested in. Once I have the first part working, I would build it up until I had a regular expression that matched everything I needed. The debugger is well suited to this sort of trial and error. When it is all working, I then just copy and paste the final regular expression into my script.
|