RegEx for matching numbers from 0.00 to 50.00 - regex

How can I get an regex expression that allows:
0.00 to 50.00 and also comma as decimal separator so
0,00 to 50,00
I have gotten as far as ([0-5]{1})?([0-9]{1})([,][0-9]{1,2})?
but there are still situations in which it fails. I have searched on line but could not find the right answer.
ADDED:
A small change in requirements. Actually it should be from 0.01 to 50.00, and 0,01 to 50,00. (But with the answers below I think I managed to adapt the regex strings so this is also matched)

There is a regex for range generator here. Generated and played with it, came up with this
^(?:[1-4]?\d[.,]\d\d?|50[.,]00?)$
Added non capturing group, little modifications and ^ start / $ end anchor. See demo at regex101.
For optional decimal part, how about this: ^(?:[1-4]?\d(?:[.,]\d\d?)?|50(?:[.,]00?)?)$

This regex should cover almost all cases matching numbers from 0.00 to 50.00,
^(?=.)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Explanation:
^ - Start of string
(?=.) - Positive look ahead to avoid matching empty string
(?: - Start of first non-grouping pattern
(?: - Start of second non-grouping pattern
(?:0|[1-4]?\d)? - This matches whole number from 0 to 49 and this whole number could be absent
(?:[,.]\d{1,2})? - Matches a comma or dot followed by one or two digits and this decimal part can be absent
) - Closing of second non-grouping pattern
| - Alternation for number 50 case
50(?:[.,]00?)?) - Matches 50 followed by either comma or dot followed by 0 or 00 where decimal part is optional
) - Closing of first non-grouping pattern
$ - End of string
Regex Demo
Edit:
For discarding zero value numbers, you can just add a negative lookahead (?!0*[.,]?0*$) in current regex and use this regex,
^(?=.)(?!0*[.,]?0*$)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Regex Demo rejecting zero valued numbers

does this one work for you?
(50([.,]0{1,2})?)|([0-4]?[0-9]([.,][0-9]{1,2})?)

Related

Regex for phone number allowing 9 or 10 digits

I am trying to validate a regex which
allows 10 digits
if digit starts with 672 then it should only allow total 9 digits
I have tried below regex
/^\d{10}$|^(672)\d{6}$/
https://regex101.com/r/0ahnKx/1
It works for 10 digits but if number starts with 672 then also it allows 10 digits.
Could anyone help how can I fix this?
Thanks!
First of all, the capturing group in your regex is redundant, it would make sense to wrap the part of the pattern between ^ and $ to only use single occurrences of the anchors.
To fix the issue, you need to make sure the first three digits matched by \d{10} are not 672, and you can achieve that with a negative lookahead:
/^((?!672)\d{10}|672\d{6})$/
/^(?:(?!672)\d{10}|672\d{6})$/
See the regex demo. Details:
^ - start of string
(?: - start of a group:
(?!672)\d{10} - no 672 substring check is triggered and then ten digits are matched
| - or
672\d{6} - 672 and six digits
) - end of the group
$ - end of string.

Regex (PCRE): Match all digits in a line following a line which includes a certain string

Using PCRE, I want to capture only and all digits in a line which follows a line in which a certain string appears. Say the string is "STRING99". Example:
car string99 house 45b
22 dog 1 cat
women 6 man
In this case, the desired result is:
221
As asked a similar question some time ago, however, back then trying to capture the numbers in the SAME line where the string appears ( Regex (PCRE): Match all digits conditional upon presence of a string ). While the question is similar, I don't think the answer, if there is one at all, will be similar. The approach using the newline anchor ^ does not work in this case.
I am looking for a single regular expression without any other programming code. It would be easy to accomplish with two consecutive regex operations, but this not what I'm looking for.
Maybe you could try:
(?:\bstring99\b.*?\n|\G(?!^))[^\d\n]*\K\d
See the online demo
(?: - Open non-capture group:
\bstring99\b - Literally match "string99" between word-boundaries.
.*?\n - Lazy match up to (including) nearest newline character.
| - Or:
\G(?!^) - Asserts position at the end of the previous match but prevent it to be the start of the string for the first match using a negative lookahead.
) - Close non-capture group.
[^\d\n]* - Match 0+ non-digit/newline characters.
\K - Resets the starting point of the reported match.
\d - Match a digit.

Validating User Input While Typing using RegEx

I am struggling to write the RegEx for the following criteria:
The number can be positive / negative
Optional - at the start
Between 1 and 5 numbers before the decimal point
2 decimal places only (optional)
Stop user from typing more than 1 . or -
This is the regex I have tried to implement which does not work for me.
^((-?[0-9]{1,5}(\.?){1,1}[0-9]{0,2})
It should allow the user to type out the following numbers.
-1.12
12345
1
123
12.12
Any help would be appreciated!
You may use
^-?\d{0,5}(?:(?<=\d)\.\d{0,2})?$
See the regex demo.
Details
^ - start of string
-? - an optional -
\d{0,5} - zero to five digits
(?:(?<=\d)\.\d{0,2})? - an optional sequence of
(?<=\d) - there must be a digit immediately to the left of the current location
\. - a dot
\d{0,2} - zero, one or two digits
$ - end of string.
If you want to validate while typing, you could make use of optional groups to accept intermediate values and do a final check on the whole pattern when processing the value.
^-?(?:\d{1,5}(?:\.\d{0,2})?)?$
Explanation
^ Start of string
-? Optional hyphen
(?: Non capture group
\d{1,5} Match 1-45 digits
(?: Non capture group
\.\d{0,2} Match a dot and 0-2 digits
)? Close group and make it optional
)? Close group and make it optional
$ End of string
Regex demo
To validate the final pattern, you could match an optional -, 1-5 digits and an optional decimal part:
^-?\d{1,5}(?:\.\d{1,2})?$
Regex demo
The regex ^(-?(\d{1,5}(\.\d{0,2})?)?)$ should work if you want to match strings that end in . such as 123. demo of this regex
Otherwise, change the 0 to a 1 as follows: ^(-?(\d{1,5}(\.\d{1,2})?)?)$. Then it will only match strings that have a digit after the decimal point.
The regex that you posted allows strings with more than 2 digits after the decimal point because it stops matching after the 2 digits, even if the string continues. Adding a $ at the end of the regex stops it from matching strings that continue after the part we want.
This regex ^(-?\d{1,5}(\.\d{0,2})?)$ will validate the input once the user has finished typing, because I assume that you don't want -to be valid at that point.

Regex range between 0 and 100 including two decimal

I'm trying to figure out a regex expression that does the following. Both conditions below must be true:
1) Between 0 and 100 inclusive
2) Can contain one or two decimals only but not obligatory.
It should not allow 100.01 or 100.1
100 is the maximum value, or 100.0 or 100.00
I tried ^(100(?:\.00)?|0(?:\.\d\d)?|\d?\d(?:\.\d\d)?)$
which helped me in this question
but this does not accept 99.0 (one decimal).
I'm probably very close.
You just need to make each second decimal digit optional:
^(?:100(?:\.00?)?|\d?\d(?:\.\d\d?)?)$
^ ^
See the updated regex demo. The 0(?:\.\d\d)? alternative is covered by \d?\d(?:\.\d\d)? one (as per Sebastian's comment) and can thus be removed.
The ? quantifier matches one or zero occurrences of the subpattern it quantifies.
Pattern details:
^ - start of string
(?: - start of an alternation group:
100(?:\.00?)? - 100, 100.0 or 100.00 (the .00 is optional and the last 0 is optional, too)
\d?\d(?:\.\d\d?)? - an optional digit followed by an obligatory digit followed with an optional sequence of a dot, a digit and an optional digit.
) - end of the alternation group
$ - end of string.
BONUS: If the number can have either . (dot) or , (comma) as a decimal separator, you can replace all \. patterns in the regex with [.,]:
^(?:100(?:[.,]00?)?|\d?\d(?:[.,]\d\d?)?)$

Regex for positive float numbers

For example:
10
0.1
1.23234
123.123
0.000001
1.000
.3
And wrong examples:
0001.2
-12
-1.01
+2.3
EDIT: standart JavaScript regex.
Try this here
^(?:[1-9]\d*|0)?(?:\.\d+)?$
See it here online on Regexr
If matching the empty string is not wanted, then you can add a length check to your regex like
^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$
The positive lookahead (?=.+) ensures that there is at least 1 character
This will pass all your test cases, multi-line mode enabled:
/^(?!0\d)\d*(\.\d+)?$/mg
Explanation:
/^ # start of regex and match start of line
(?!0\d) # not any number with leading zeros
\d* # consume and match optional digits
(\.\d+)? # followed by a decimal and some digits after, optional.
$ # match end of line
/mg # end of regex, match multi-line, global match
RegExr: http://regexr.com?2tpd0
Consider the regular expression:
^[0-9]*(?:\.[0-9]*)?$
This regular expression will matches floating point number like:
- .343
- 0.0
- 1.2
- 44
- 44.
- 445.55
- 56.
- . //Only dot(.) also matches
- empty string also matches
The above regular expression will will not accept:
- h32.55 //Since ^ is used. So, the match must start at the beginning
of the string or line.
- 23.64h //Since $ is used. So, the match must occur at the end of the string or before \n at the end of the line or string.
Consider the regular expression:
^[0-9]+(?:\.[0-9]+)?$
This regular expression will matches floating point number like:
- 45
- 45.5
- 0.0
- 1.2
- 445.55
This regular expression will not accept:
- h32.55 //Since ^ is used. So, the match must start at the beginning
of the string or line.
- 23.64h //Since $ is used. So, the match must occur at the end of the string or before \n at the end of the line or string.
- 44.
- . //Only dot(.) does not matches here
- empty string also does not matches here
Pure floating point:
^(([0-9]+(?:\.[0-9]+)?)|([0-9]*(?:\.[0-9]+)?))$
You can check the regular expression here.
Refer MSDN page for additional information.
I've stumbled on this page a few times, here is my solution for any one who stumbles here after me:
A regex like a=(\d+\.?\d* | \d*\.?\d+) matches all decimals numbers without a sign but includes things like 002.0
A regex to filter those things are b=[1-9\.]+.*
So one solution is to say it matches the criteria if a & b matches. Or equivalently (contrapositive), see if there is no match for !a | !b. Unfortunately, most languages don't have a complete regex package; the 'and' and negate functions of regular languages isn't present usually. Two simple regexes I've found in code looks a lot nicer and are more maintainable than one complex one (I say this in context to this question & similar situations)