Re: regex help

From: Roberto Etcheverry (rob_at_arkham.com.ar)
Date: 05/28/04


Date: Fri, 28 May 2004 18:21:41 -0300 (ART)
To: beginners <beginners@perl.org>


On Fri, 28 May 2004, Andrew Gaffney wrote:

> I'm trying to write a regex to parse the following data. Each group is a string
> to parse.
>
> <td class="f3" colspan="2" width="48">05/28/04</td>
> <td class="f3" colspan="2" width="60"></td>
> <td class="f3" colspan="2" width="186">Purchase With Pin Pin</td>
> <td class="f3" colspan="2" align="right" width="78"></td>
> <td class="f3" colspan="2" align="right" width="78">$10.00<br>(pending)<a
> href='javascript: ShowHelp("PENDING TRANSACTION")'><img src="usb
> ank_files/help.gif" valign="middle" alt="Pending Transaction Help"
> border="0"></a></td>
> <td class="f3" align="right">$1,224.45</td>
>
> <td class="f3" colspan="2" width="48">05/27/04</td>
> <td class="f3" colspan="2" width="60"></td>
> <td class="f3" colspan="2" width="186">Purchase With Pin Shell Service Stlake
> St. Loumo</td>
> <td class="f3" colspan="2" align="right" width="78"></td>
> <td class="f3" colspan="2" align="right" width="78">$1.78</td>
> <td class="f3" align="right">$1,234.45</td>
>
> <td class="f3" colspan="2" width="48">05/21/04</td>
> <td class="f3" colspan="2" width="60"></td>
> <td class="f3" colspan="2" width="186">Atm Withdrawal One O'fallon Squo'fallon
> Mo 1</td>
> <td class="f3" colspan="2" align="right" width="78"></td>
> <td class="f3" colspan="2" align="right" width="78">$20.00</td>
> <td class="f3" align="right"><a href='javascript:
> ShowHelp("NOTE","RESTRICTEDFUNDSAMOUNT=$2.00","AVAILABLETRANSACTIONAMOUNT=$1,132.79")'>$
> 1,134.79</a></td>
>
> This is the regex I put together:
>
> my $regex = '<td[^>]+?>(\d{2})/(\d{2})/(\d{2})</td>.+?';
> $regex .= '<td[^>]+?>(.*?)</td>.+?';
> $regex .= '<td[^>]+?>(.+?)</td>.+?';
> $regex .= '<td[^>]+?>(?:\$(\d+\.\d{2})).*?</td>.+?';
> $regex .= '<td[^>]+?>(?:\$(\d+\.\d{2})).*?</td>.+?';
> $regex .= '<td[^>]+?>.*?(?:\$(\d+\.\d{2})).*?</td>';
>
> The first field will always be in the form 'mm/dd/yy'. The second and third
> field need to be captured as they are. As for the fourth and fifth fields, only
> one will contain a value. The other one will be empty (nothing between
> <td></td>). The format is '$123.45' with the possibility of trailing HTML before
> the </td>. I only want the number without the $. The sixth field will contain a
> dollar amount like the fourth and fifth fields. It could be surrounded by HTML.
> Again, I only need the number without the $. What is wrong with the above regex?
> I am using it with the 's' modifier.
>
> --
> Andrew Gaffney
> Network Administrator
> Skyline Aeronautics, LLC.
> 636-357-1548
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
It seems two things are missing:

1) A '?' after the 4th and 5th group (because they may be empty).
2) Include ',' on the regex matching the amounts (to match '1,234.45' for
example).

So the regex would be:

my $regex = '<td[^>]+?>(\d{2})/(\d{2})/(\d{2})</td>.+?';
$regex .= '<td[^>]+?>(.*?)</td>.+?';
$regex .= '<td[^>]+?>(.+?)</td>.+?';
$regex .= '<td[^>]+?>(?:\$([\d,]+\.\d{2}))?.*?</td>.+?';
$regex .= '<td[^>]+?>(?:\$([\d,]+\.\d{2}))?.*?</td>.+?';
$regex .= '<td[^>]+?>.*?(?:\$([\d,]+\.\d{2})).*?</td>';



Relevant Pages

  • Re: displaying ASCII value of character
    ... Andrew Gaffney wrote: ... There's obviously something there if it's causing ... a carriage return in the output. ... Shouldn't the second regex catch whatever is there? ...
    (perl.beginners)
  • Re: regex for l33t speak
    ... >> I'm trying to come up with a regex for my IRC bot that detects 1337 ... >> one that will have few false positives but also work most of the time. ... Andrew Gaffney ...
    (perl.beginners)
  • RE: regex for l33t speak
    ... Andrew Gaffney wrote: ... Ever considered doing this w/o a regex? ... letters and numbers using tr//, ... HTH, ...
    (perl.beginners)
  • Parsing and regex
    ... I am writing a program to parse a Cisco ASA log file. ... I have created a database that contains the 6 ... digit rule number which is unique, a severity level, the full ASA rule ... regex "rule" in the database. ...
    (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)