1

I'm using this regex to match some strings:

^([^\s](-)?(\d+)?(\.)?(\d+)?)$/

I'm confusing about why it's permitted to enter two dots, like ..

What I understand is that only allowed to put 1 dash or none (-)? Any digits with no limit or none (\d+)? One dot or none (\.)?

Why is allowed to put .. or even .4.6?

Testing done in http://www.regextester.com/

1
  • What are the strings you want to match and which are that you do not want to match? [^\n] is a negated character class that matches .. Add it to [^\n.], and it will not match. Commented Aug 11, 2015 at 14:17

3 Answers 3

5

[^\s] means anything that is not a whitespace. This includes dots. Trying to match .. will get you:

  1. [^\s] matches .
  2. (-)? doesn't match
  3. (\d+)? doesn't match
  4. (\.)? matches .
  5. (\d+)? doesn't match


I'll assume you wanted to match numbers (possibly negative/floating):

^-?\d+(\.\d+)?$
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly right. Regexplained might give some clarity
Omg, how can I didn't notice it. Thank you!
3
^([^\s](-)?(\d+)?(\.)?(\d+)?)$/

Created with RegexBuddy

1 Comment

Yeah, the reputation goes to the tool :)
1

As I mentioned in my comment, [^\n] is a negated character class that matches .. and as there is another (\.)? pattern, the regex can match 2 consecutive dots (since all of the parts except for [^\s] are optional).

In order not to match strings like .4.5 or .. you just need to add the . to the [^\n] negated character class:

^([^\s.](-)?(\d+)?(\.)?(\d+)?)$
      ^

See demo. This will not let any . in the initial capturing group.

You can use a lookahead to only disallow the first character as a dot:

^(?!\.)([^\s](-)?(\d+)?(\.)?(\d+)?)$

See another demo

All explanation is available at the online regex testers:

enter image description here

In order to match the numbers in the format you expect, use:

^(?:[-]?\d+\.?\d*|-)$

Human-readable explanation:

  • ^ - start of string and then there are 2 alternatives...
  • [-]? - optional hyphen
  • \d+ - 1 or more digits
  • \.? - optional dot
  • \d* - 0 or more digits
  • | -OR-
  • - - a hyphen
  • $ - end of string

See demo

9 Comments

What I trying to do is match any decimal number including negatives, but not empty string. 1 - Allowed -1 - Allowed 1. - Allowed -1. - Allowed 1.0 - Allowed -1.0 - Allowed
Sorry, '-' needs to be valid too. '-' Allowed '-.' not allowed '.' not allowed Please don't spent your whole time, you have already helped a lot
I'm doing a validation on keyup, user is writting the decimal number, he can starts with negative '-', but he can't start with dot '.', he can finish with dot, but he can't put more than one dot or more than one dash and not a dash before a dot.
Yeah, i think ^[-]?\d+\.?\d*|-$ will solve!! :) you're a good man.
@JohnnyWiller: Thanks for the reminder, I was focusing on other details and overlooked this. However, I'd rather use a non-capturing group since we are not interested in the captured text.
|

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.