Laughy
Laughy

Reputation: 1979

finding if a string contains non digits except for full stop?

I have tried many permutations but they all don't seems to work well. Am I right to say that [\D^[\.]] if used in java matcher will only match those with non digits but excludes the matching of full stop? However, it doesn't seems to work...Thanks for any input on my question~!

Upvotes: 2

Views: 365

Answers (1)

sarnold
sarnold

Reputation: 104110

It might be easier to instead make sure the string contains only digits and full stop and report an error otherwise. (Just look at it from the other way around...)

The regex for this is pretty simple:

/^[\d.]+$/

Just add a ! in front of your method call matching the input against the pattern.

Inverting the pattern is often an easy way to get what you need.

Upvotes: 4

Related Questions