NewBeee_Java
NewBeee_Java

Reputation:

What regex will match 4 digits, a period, then one and only one digit?

I need a (Java) regular expression that will match:

XXXX.X

Where X is any number, only one number after the decimal point.

Upvotes: 5

Views: 21813

Answers (2)

sujithayur
sujithayur

Reputation: 386

If the number is exactly 4 digits,then try this

"/(^([0-9]{4})[.]([0-9]{1})$)/"

Eg : 1234.4

Or if the number is of unlimited digits,try this..

"/(^([0-9]{0,})[.]([0-9]{1})$)/"

Eg: 1234.4
45.8
589745324744.7

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208435

Try ^\d{4}\.\d$ if you want the entire string to match, remove the ^ and/or $ if you want it to find matches within a larger string.

If there can be any number of integers before the . use \d+ instead of \d{4} to match one or more, or \d* to match zero or more (the string ".5" would match \d*\.\d).

Upvotes: 13

Related Questions