-1

I am currently studying Regex and I came up in this regex:

r"\₱[0-9]*[.]{,1}[0-9]{,2}"

for QRegExp is and currently It is only good if it starts with other than that my app is breaking is there a way to get these following formats:

100.0
100.00
₱100.00

2 Answers2

0

You should make optional by changing \₱ to [\₱]{0,1}

r"[\₱]{0,1}[0-9]*[.]{0,1}[0-9]{0,2}"

Note: I always use {0,1} syntax and not {,1} to ensure the next developer is clear on what it does.

Conditions

  1. May or may not start with the character
  2. Number must have a decimal point
  3. It cannot detect negative numbers
  4. It will not accept more than 2 position after the decimal point

External Regex Fiddle

https://regex101.com/r/dBk1Dq/1

Eagnir
  • 459
  • 3
  • 7
  • Thank you sir is there a manual or text book where I can read about regExp that you can recommend to me? – Jansen Lloyd Macabangun Jul 05 '22 at 07:40
  • You should use https://regex101.com, it has references, examples as well as explanation on different syntax. The best is you can try the regex right away and figure it out while changing it. – Eagnir Jul 05 '22 at 07:42
0
r"₱?\d+(?:\.[0-9]{,2})?"

The escape before the symbol is not needed. I also made the rest a bit more concise and faster, so the engine doesn't need to uselessly backtrack, by making the whole decimal section optional, but the decimal point mandatory within that group, and I replaced [0-9] with the more semantic \d. Presumably you will want at least one digit, so I took the liberty to change * to +.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • thank you for answering! I'm confused between these format r"₱?\d+(?:\.[0-9]{,2})?" and the format that is something like this: /^(\$?([0-9]*\.?\d{2}?)$)/ is there a way so that I can distinguish it ? – Jansen Lloyd Macabangun Jul 05 '22 at 07:47
  • As Eagnir said, regex101 is a good site that can explain regular expressions. Besides, there is https://www.regular-expressions.info/ as well as Stack Overflow's canonical [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). – Amadan Jul 05 '22 at 08:20