-1

I came to scenario where I only want [0-9 or .] For that I used this regex:

[0-9.]$

This regex accepts 0-9 and . (dot for decimal). But when I write something like this

1,1 

It also accepts comma (,). How can I avoid this?

Biffen
  • 6,249
  • 6
  • 28
  • 36
FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76
  • 1
    Your regex says “the input must end with a number 0-9 or a period”, it doesn’t care about anything before the last character. Change it to include all characters (+ or *) and start from the beginning (^). – Sami Kuhmonen Mar 22 '18 at 06:08
  • The dot is a wildcard: It can match any single character (letter, digit, whitespace, everything). You have to escape the dot by using a slash: `\.` – Link Mar 22 '18 at 06:11
  • The "dot" in this case means "any single character". If you want to accept only the "." your regex has to be [0-9\.]$. – kazbeel Mar 22 '18 at 06:11
  • Try like `/[0-9.]+/g` – Sinto Mar 22 '18 at 06:14
  • 3
    @Link, the dot need not be escaped inside a character class. – builder-7000 Mar 22 '18 at 06:24
  • You have already excluded "," since your regex matched twice for each 1 in "1,1", also, look here for excluded pattern : https://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string – tomjpsun Mar 22 '18 at 06:39
  • Use `^[0-9.]+$` – Wiktor Stribiżew Mar 22 '18 at 07:38

3 Answers3

1

Once you are looking into a way to parse numbers (you said dot is for decimals), maybe you don't want your string to start with dot neither ending with it, and must accept only one dot. If this is your case, try using:

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

where:

  • \d+ stands for any digit (one or more)
  • \.? stands for zero or one of literal dot
  • \d stands for any digit (just one)

You can see it working here


Or maybe you'd like to accept strings starting with a dot, which is normally accepted being 0 as integer part, in this case you can use ^\d*\.?\d+$.

guijob
  • 4,413
  • 3
  • 20
  • 39
  • 1
    This requires two or more digits. Numbers from 0 to 9 will fail. I would recommend to accept '.' at beginning (`\d*\.?\d+$`) that is universally accepted as implicit '0' at beginning. Requiring '.' not being at the beginning AND end, requires more expensive processing if you *really* require to do it with single regular expression. – bitifet Mar 22 '18 at 06:37
  • 1
    @bitifet well pointed out! I've just edited my regex in order to not allow dots in beginning and end but accepting number from `0` to `9`. Moreover, I've added a note about your consideration which I think is completely valid. Thank you – guijob Mar 22 '18 at 06:50
  • 1
    Right. And I missed the fact that two regular expressions can be composed in single one as alternatives by '|' operator (even some times I did the same!). Good job. – bitifet Mar 22 '18 at 06:59
1

This regex [0-9.]$ consists of a character class that matches a digit or a dot at the end of a line $.

If you only want to match a digit or a dot you could add ^ to assert the position at the start of a line:

^[0-9.]$

If you want to match one or more digits, a dot and one or more digits you could use:

^[0-9]+\.[0-9]+$

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

This regex may help you:

/[0-9.]+/g

Accepts 0 to 9 digits and dot(.).

Explanation:

Match a single character present in the list below [0-9.]+

+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

. matches the character . literally (case sensitive)

You can test it here

enter image description here

Sinto
  • 3,915
  • 11
  • 36
  • 70