I am searching for a RegEx for prices. So it should be X numbers in front, than a "," and at the end 2 numbers max.
Can someone support me and post it please?
The /^$/ component is a regular expression that matches the empty string. More specifically, it looks for the beginning of a line ( ^ ) followed directly by the end of a line ( $ ), which is to say an empty line.
For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
Yes, regex is expensive, so use it wisely.
A regex ( also known as regular expressions) is a pattern string. These pattern strings allow you to search specific patterns in documents and to validate email, phone number etc. In iOS and MacOS regex been handled by NSRegularExpression . To know more about NSRegularExpression read apple documentation.
In what language are you going to use it?
It should be something like:
^\d+(,\d{1,2})?$
Explaination:
X number in front is: ^\d+
where ^
means the start of the string, \d
means a digit and +
means one or more
We use group ()
with a question mark, a ?
means: match what is inside the group one or no times.
inside the group there is ,\d{1,2}
, the ,
is the comma you wrote, \d
is still a digit {1,2}
means match the previous digit one or two times.
The final $
matches the end of the string.
I was not satisfied with the previous answers. Here is my take on it:
\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})
|^^^^^^|^^^^^^^^^^^^^|^^^^^^^^^^^|
| 1-3 | 3 digits | 2 digits |
|digits| repeat any | |
| | no. of | |
| | times | |
(get a detailed explanation here: https://regex101.com/r/cG6iO8/1)
Covers all cases below
But also weird stuff like
In case you want to include 5 and 1000 (I personally wound not like to match ALL numbers), then just add a "?" like so:
\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With