Thursday, June 01, 2006

I HATE Regular Expressions

So I've been in vain trying to look for a regular expression that validates dollar amounts but doesn't allow any other symbols other than "+", "-" and "." (no commas either please).

I refuse to figure out how to build it, it's bad enough simple ones are beyond my comprehension (read: I have no patience, and frankly, no time to figure this out). So I'm just not going to put the validation in right now, I still have too much to do.

For once, Internet is failing me. Waaaaah!!! If anyone has any bright ideas, please let me know.

(And for those who haven't got a clue as to what I'm talking about, trust me, you don't wanna know).

2 comments:

Anonymous said...

Well, this will only help if you're using Perl style regular expressions:

\$(\d*)(\.)?(\d{2})?

or

\$(\d*)(\.)?(\d\d)?

To explain it to those of you who can't read it like it's english, the backslashes either "escape" a special character, or create one. The dollar sign is a special character, so we want it to have it's regular meaning, thus \$, we then want it to be followed by zero or more digits (e.g. numbers). These are represented by "\d", the asterix means "zero or more". We then want to allow for a decimal point, which in this case is a period, get rid of it's special meaning (typical matches any character) and it becomes "\.", we enclose it in parenthesis so that our next operator acts on the parenthesis. In this case the "?", which for all intents and purposes here indicates optional, but technically means "zero or one". The bit is just two ways of writing that we want the decimal to be followed by exactly 2 digits, or none at all.

Now more properly they should probably be something like this:

\$\s*(\d*)(\.\d{2})?

This one allows whitespace between the dollar sign and the first digit, and requires that the decimal point is followed by two digits. It however has the flaw that it would accept just a dollar sign as a hit as well, since all the other items are technically optional.

Bottom line is that I'd need to see a real idea of what's trying to be included and what's trying to be excluded, and what language you're using to be truly useful.

P.S. Regular expressions are beautiful things, truly majestic and infinitely powerful. Sort of like Jean Grey, they do require that appropriate boundaries are set, but beyond that the possibilities are literally endless.

Irene said...

Thanks for the help DC. I realize that RE's are a beautiful thing, and are very powerful. It's just that I'm a little short on patience these days, as you well know, you hear me rant enough =P