Member Avatar for Zurompeta

Hi all,

Been at this for a while now and finally gave in to seek help.

Basically, I'm trying to setup a regular expression for a decimal. Where there's 1 to 7 integers in front of the decimal point, and 1 to 2 after the decimal point.

This is what I got so far:

"/(\d{1,7})\.(\d{1,2})/"

It ALMOST works.

This number should be the absolute maximum that can match: 1234567.12

These numbers are matching, when they shouldn't be

12345678.12
1234567.123

As you can see, it's matching passed the "maximum".

Any ideas?

Member Avatar for ShawnCplus

"/^(\d{1,7})\.(\d{1,2})$/" If you're testing a string you want to set the anchors "^" (beginning of string) and "$" (end of string) so you know that there is nothing else in between. "/(\d{1,7})\.(\d{1,2})/" will match 12345678.12 because it is matching, take a look at this.

without ^$ __________ 1|2345678.12| ---------- that's 7 numbers a decimal and 2 numbers, so a match with anchors _______ |1234567|8.12 ------- no match
Member Avatar for Zurompeta

That did the trick!

Thanks for the answer as well as the explanation. Definitely good to know! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.