# RegExp (£n.nn) test

## Recommended Posts

This is my first RegExp pattern, but I cannot figure out how to make it an exact match.

```\$Money1 = "£22.50"
\$Money2 = "£6.00"

\$MoneyPattern = "£\d+\.\d+"

ConsoleWrite(StringRegExp(\$Money1, \$MoneyPattern, 0) & @LF)
ConsoleWrite(StringRegExp(\$Money2, \$MoneyPattern, 0) & @LF)```

It's suppose to test the a string is an English monetary value, and it appeared to work, until I fed it "£22.50x"

Here's what I think it does.

The string must begin with "£"

Then it must have 1 or more digits [0-9]

Those digits must have  a decimal point "." immediately after them

Then must be followed by 1 or more digits.

I cannot figure out how to tell the pattern that the decimal point "." must be followed by exactly 2 digits.

I'd appreciate an explanation if anyone has the time.

Monkey's are, like, natures humans.

##### Share on other sites

This one works for me in your example.  Haven't done any exhaustive testing.

`"^£\d+\.\d{2}"`

The ^ means that the pound sign will appear at the beginning.  The {2} means there have to be 2 numbers.

Edit: Seems that {2} isn't limiting the regex to exactly 2 numbers.

Edit: This will allow an x but won't limit it to one for some reason:

`"^£\d+\.\d{2}x{0,1}"`

Edit: Allright, well, I'm off the mark, but that's a start.

Edited by jaberwacky

##### Share on other sites

```\$Money1 = "£22.50"
\$Money2 = "£6.00"
\$Money3 = "£22.50x"

\$MoneyPattern = "(£\d+\.\d\d\z)"

\$match = StringRegExp(\$Money1, \$MoneyPattern, 0) ?  msgbox(0, '' , \$Money1) : msgbox(0, '' , 'no match found')
\$match = StringRegExp(\$Money2, \$MoneyPattern, 0) ?  msgbox(0, '' , \$Money2) : msgbox(0, '' , 'no match found')
\$match = StringRegExp(\$Money3, \$MoneyPattern, 0) ?  msgbox(0, '' , \$Money3) : msgbox(0, '' , 'no match found')```

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites

That appears to work boththose.

I did try the "{2}" you posted Jaber, wondering why it never worked.

EDIT:

Thought I'd give this a go to see if it worked with {2}

```\$Money1 = "£22.54"
\$Money2 = "£0.076"

\$MoneyPattern = "^(£\d+\.\d{2}\z)"

ConsoleWrite(StringRegExp(\$Money1, \$MoneyPattern, 0) & " " & @error & " " & @extended & @LF)
ConsoleWrite(StringRegExp(\$Money2, \$MoneyPattern, 0) & " " & @error & " " & @extended & @LF)```

And it appears though it does.

Ace.

Cheers men.

EDIT2:

I might have to leave out the "^" though, to allow for negative values "-£123.45"

Edited by JohnOne

Monkey's are, like, natures humans.

##### Share on other sites

I just done another 2, and I'm all atwitter about it because I think I'm getting the hang of the basics.

I'm am though, seasoned enough to know I could well be doing them wrong even though they appear to work.

If anyone would care to take a look at these patterns, for the date formats I have, I'd be mighty obliged if you could point out where I might be doing something wrong.

```\$Date1 = "17/08/2013 1.12 PM"
\$Date1Pattern = "^(\d\d\/\d\d\/\d{4} \d{1,2}\.\d\d [PAM]{2})"

\$Date2 = "09-Jan-2011 21:33:30"
\$Date2Pattern = "^(\d\d-[A-Za-z]{3}-[0-9]{4} \d\d\:\d\d\:\d\d)\z"```

Monkey's are, like, natures humans.

##### Share on other sites

Well, they'd work, but they could be slightly stricter. The first one, for instance, would let PP, PA, AP, AA, MP and MA pass as well. "[PA]M" instead of "[PAM]{2}" would be better. And the second one would allow for a time 99:99:99, a date "99-xlZ-9998". How about:

`\$Date2Pattern="^([0-3]?[0-9]-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4} [0-2][0-9]:[0-5][0-9]:[0-5][0-9])\z"`

It still allows for 39 feb but it's a lot stricter. You could prefix the pattern with (?i) to make it case insensitive.

/edit: you can use this crude approach if you know what you're looking for in what data and that the chance for false positives is zero. Or if you know that false positives aren't problematic, like if you're grepping through application logfiles, though it's bad logging if it doesn't match year->month->day->hour->min->sec(->msec)

But if you need to have 100% matching you would need a much more cumbersome regex or, better in this case, some extra parsing and error checking (like matching these crude regexes, then taking the string apart, putting it into "YYYY/MM/DD[ HH:MM:SS]" format and checking whether _DateTimeFormat can make sense of it.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

Thanks, I understand I can use what I have, and I'm 99.99% certain they would be fine after you pointed out what could go wrong, but I'm also trying to learn RegEx and so I really appreciate you thoughts.

Monkey's are, like, natures humans.

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...