Friday, June 17, 2011

more RegEx

The more I use Regular Expressions, the more I rely upon the ease of use they provide in pattern matching in real, everyday work.
Want to match a date at the start of a line?  try

^\d*/\d*/\d* \d*:\d*:\d* [A|P]M
The ^ means the start of a line.
The \d matches all numbers
The [A|P] matches to either "A" or "P" as in AM/PM.
The rest are just string literals.

I was needing to clean a log file I extracted from the Windows Event log to report a problem to our IT support staff.
I had several hundred line s like
The description for Event ID '1073741850' in Source 'Application Popup' cannot be found.  The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them.
Where each ID was different.  Solution?  RegEx and Repetition!
I used this as my search query.
The description for Event ID '\d*' in Source 'Application Popup' cannot be found.  The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them.  The following information is part of the event:
The \d is, of course, the shorthand meaning "numeric information."  this is the same as the expression [0-9$.,]
* says "repeat the last search term 0 or more times.

No comments: