niksvp
niksvp

Reputation: 5563

Problem in regex for decimal number

I want a regex for decimal numbers like 00.0

I tried this [0-9]{1,2}(.[0-9]{1})? which works perfectly.

But I have to add ^ at begining and *$ at end.

Is there any way to have the regex work as the one working along with adding these characters?

^([0-9]{1,2}(.[0-9]{1})?)*$ --> fails to do what I want.

My regex should look like ^[Anything here]*$

Any help would be appreciated.

Thanks

Upvotes: 1

Views: 5277

Answers (4)

niksvp
niksvp

Reputation: 5563

I figured out the problem was * and it could be excluded by adding a pair of parenthesis before * like ()*

And ^([0-9]{1,2}(\.[0-9])?)()*$ works well.

Upvotes: 1

Toto
Toto

Reputation: 91385

If I understand well what you need, have a try with :

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

This will match

\^       a carret ^
\d\d?    1 or 2 digit
(\.\d)?  eventually a dot and a digit
\*       an asterisk
\$       a dollar

Upvotes: 1

Pablitorun
Pablitorun

Reputation: 1025

I think you need .* at the end

but could you reply with some examples of strings you want to match and ones you don't want to match>

Upvotes: 1

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136238

Depends on the type of regex, but for many regex types (posix, posix extended, perl, python, emacs) . (dot) means match any symbol. To match the dot symbol you need to quote it like \..

And to match exactly one digit you don't need to add {1} at the end of it. I.e. [0-9]{1} is the same as [0-9].

Upvotes: 1

Related Questions