How to pass Philippine Currency in QRegExp?

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

Upvotes: -1

Views: 53

Answers (2)

Amadan
Amadan

Reputation: 198476

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 +.

Upvotes: 0

Eagnir
Eagnir

Reputation: 489

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

Upvotes: 0

Related Questions