0
^[0-9]\\d*(\\.\\d+)?$

I can't quite work out what the above regex pattern is looking for. I'm tempted to interpret it as "find anything that is not the numbers 0-9 inclusive, then find zero or more occurrences of a single digit, then find zero or one occurrences of a decimal point followed by at least one digit" but I'm not sure.

Part of my confusion stems from the fact that in the SCJP6 certification book, the not operator is included inside the square brackets, whereas here it's outside. Also, I am just generally inexperience when it comes to regex.

Can someone please help? [This is from a Java program. Is the above in any way Java specific?] Thanks.

3 Answers 3

3

^ start of a string

[0-9] a single digit

\\d* any amount of digits (0-infinity)

(\\.\\d+)? Once, or not at all: a dot followed by at least one digit

$ end of string.

You have a complicated regex that will match any floating point or non floiting point number.

Have a look at the java.util.Pattern class and and the Oracle Java Regex Tutorial.

Sign up to request clarification or add additional context in comments.

Comments

2

It is looking a one or more digits, optionally followed by a . and one or more digits. It is confusing as it is needlessly complicated. It is the same as

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

\d is defined as A digit: [0-9]

Comments

1

When the "^" operator is outside of a character class "[]" it denotes the start of input, "$" defines end of input.

So your description is correct, but it should be changed to:

find a single digit from zero to nine...

for more information about regular expressions check out this link

2 Comments

So what is the difference between [0-9] and \\d?
There is no difference between [0-9] and \\d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.