in reply to REGEX Frustration

RobertJ:

You may simply be working too hard. If your format is just as described, and you don't have the TAB character anywhere in your data, you could probably just use something like (untested):

while (my $line = <INFILE>) { my ($date, $description, $amount) = split /\t/, $line; if (defined $amount) { print OUTFILE $amount; } else { # Amount not found... } }

If you're really wanting to use a regular expression, though, something like this might be more appropriate:

while (my $line = <INFILE>) { if ($line =~ /(-?\$\d*\.\d*)/) { print OUTFILE $1, "\n"; } else { # Amount not found... } }

In the regex, '-?' means "Maybe a minus sign". I left out the square brackets because they didn't really add anything just make a mess of things.

...roboticus

Update: Corrected square bracket comment. (Kudos to GrandFather for the catch.)

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: REGEX Frustration
by GrandFather (Saint) on Dec 22, 2011 at 21:24 UTC

    Actually leaving out the square brackets is required! Not so much they didn't add anything as, they subtracted a lot! By now maybe the OP has read the regex docs and will realise that the square brackets denote a set of characters which matches just one character in the string being matched. It looks like the OP was thinking [] is equivalent to (). It's not.

    True laziness is hard work