-1

i write a regex for integers and float in Qt (C++) Now i dont want to match my Input when i write a comma. For example:

  1. 123.3 --> ok
  2. 123 --> ok
  3. 123,3 --> NO match

I tried the following regex:

(\d*[.]?\d*)

so now i match for:

  1. 123.3 --> match: 123.3
  2. 123 --> match: 123
  3. 123,3 --> match: 123

I dont want to match the regex when i write a comma. Is this possible?

Kirikkayis
  • 83
  • 7

2 Answers2

0

In a regular expression, a period . means "match any character". To match a literal period, you need to escape it, as in \.. So, something along the lines of

\d+(\.\d*)?
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • when i Input 123,3 i matchwith your regex 123 and 3. So i match 2 numbers. You can try it at https://regex101.com/ – Kirikkayis Jun 18 '19 at 12:05
  • I'm not sure what you mean by "input with your regex". Show the code where you apply the regex, if you want help with that code. – Igor Tandetnik Jun 18 '19 at 12:05
  • try it on https://regex101.com/ … when i enter 1234,3 with your regex i match 2 numbers. The first one is 1234 and the secound is 3. I dont want to match anything when i collect a comma. – Kirikkayis Jun 18 '19 at 12:07
  • 1
    regex101.com by default matches in global mode, applying the expression repeatedly to the whole string. Either change the mode (a button at the far right), or force whole-string match with `^\d+(\.\d*)?$` – Igor Tandetnik Jun 18 '19 at 12:08
0

Okay, now i find the soulution. Sorry :)

I had to add a start and end of line Symbols …

^(\d*[.]?\d*)$
Kirikkayis
  • 83
  • 7