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.
Related
I'm trying to create a currency Regex using the Robin Herbots Inputmask plugin and can't find a way to make it all work together
I need a regex that allows precision from 2 to 10 digits and adds a comma on the thousands, like these:
1,123.00
123.12345
1,123,456.1234567890
and so on. I ended up with these regexes ^\\d{1,3}(?:,\\d{3})*$ and \d{1,99},\d{2,10}. They both achieve what I need, but separately. How to I make them both work together?
Thanks!
You can use
/^(?=(?:,?\d){1,99}(?:\.|$))\d{1,3}(?:,\d{3})*(?:\.\d{2,10})?$/
See the regex demo.
Details:
^ - start of string
(?=(?:,?\d){1,99}(?:\.|$)) - a positive lookahead that requires 1 to 99 occurrences of an optional , followed with a digit, and then followed with either . or end of string
\d{1,3} - one to three digits
(?:,\d{3})* - zero or more occurrences of a comma and then three digits
(?:\.\d{2,10})? - an optional occurrence of a . and two to ten digits
$ - end of string.
I am trying to block any strings that contain more than 3 numbers and prevent special characters. I have the special characters part down. I'm just missing the number part.
For example:
"Hello 1234" - Not Allowed
"Hello 123" - Allowed
I've tried the following:
/^[!?., A-Za-z0-9]+$/
/((^[!?., A-Za-z]\d)([0-9]{3}+$))/
/^((\d){2}[a-zA-Z0-9,.!? ])*$/
The last one is the closest I got as it prevents any special characters and any numbers from being entered at all.
I've looked through previous posts, but am coming up short.
Edit for clarification
Essentially I'm trying to find a way to prevent customers from entering PII on a form. No submission should be allowed that contains more than 3 numbers in a string.
Hello1234 - Not allowed
12345 - Not allowed
1111 - not allowed
No where in the comment section when the user enters the string should there be more than 3 numbers in total.
About the patterns that you tried
^[!?., A-Za-z0-9]+$ The pattern matches 1+ times any of the listed, including 1 or more digits
((^[!?., A-Za-z]\d)([0-9]{3}+$)) If {3}+ is supported, the pattern matches a single char from the character class, 1 digit followed by 3 digits
^((\d){2}[a-zA-Z0-9,.!? ])*$ The pattern repeats 0+ times matching 2 digits and 1 of the listed in the character class
You can use a negative lookahead if that is supported to assert not 4 digits in a row.
^(?!.*\d{4})[a-zA-Z0-9,.!? ]+$
regex demo
If there can not be 4 digits in total, but 0-3 occurrences:
^[a-zA-Z,.!? ]*(?:\d[a-zA-Z,.!? ]*){0,3}$
Explanation
^ Start of string
[a-zA-Z,.!? ]* Match 0+ times any of the listed (without a digit)
(?:\d[a-zA-Z,.!? ]*){0,3} Repeat 0 - 3 times matching a single digit followed by optional listed chars (Again without a digit)
$ End of string
regex demo
If you don't want to match an empty string and a lookahead is supported:
^(?!$)[a-zA-Z,.!? ]*(?:\d[a-zA-Z,.!? ]*){0,3}$
See another regex demo
Here is my two cents:
^(?!(.*\d){4})[A-Za-z ,.!?\d]+$
See the online demo
^ - Start string anchor.
(?! - Open a negative lookahead.
( - Open capture group.
.*\d - Match anything other than newline up to a digit.
){4} - Close capture group and match it 4 times.
) - Close negative lookahead.
[A-Za-z ,.!?\d]+ - 1+ Characters from specified class.
$ - End string anchor.
I think it should cover what you described.
Assuming you mean <= 3 digits, this may be a naive one but how about
[ALLOWED_CHARS]*[0-9]?[ALLOWED_CHARS]*[0-9]?[ALLOWED_CHARS]*[0-9][ALLOWED_CHARS]*?
Fill [ALLOWED_CHARS] to whatever you define is not special character and nums.
Remove trailing zeros to a number with 4 decimals
Sample expected output:
1.7500 -> 1.75
1.1010 -> 1.101
1.0000 -> 1
I am new with REGEX so I just tried this one first but not working:
REPLACE ALL OCCURRENCES OF REGEX '^\.[0]\d{0,3}' IN lv_rate WITH space.
Need help for the right regex to use. Thanks!
EDIT: SHIFT lv_rate RIGHT DELETING TRAILING '0' is not an option.
Try replacing on the following regex pattern:
\.?0+$
Use empty string as the replacement. This will match an optional decimal point, followed by trailing zeroes until the end of the string. See the demo below to see this pattern working.
Demo
This answer assumes that all inputs would always have a decimal component. If not, then we would need to add additional logic.
If you want to remove trailing zeros to a number with 4 decimals, one option is to use a capturing group and use group 1 in the replacement.
^(\d+(?=\.\d{4}$)(?:\.\d*[1-9])?)\.?0+$
In parts
^ Start of string
( Capture group 1
\d+ Match 1+ digits
(?=\.\d{4}$) Assert what is on the right is a . and 4 digits
(?:\.\d*[1-9])? Optionally match digits until the last digit 1-9
) Close group 1
\.?0+ Match an optional . and 1 or more times a zero
$ End of string
Regex demo
I'm trying to match an exact pattern to do some data cleanup for ISSN's using the code below:
select case when REGEXP_REPLACE('1234-5678 ÿþT(zlsd?k+j''fh{l}x[a]j).,~!##$%^&*()_+{}|:<>?`"\;''/-', '([0-9]{4}[\-]?[Xx0-9]{4})(.*)', '$1') not similar to '[0-9]{4}[\-]?[Xx0-9]{4}' then 'NOT' else 'YES' end
The pattern I want match any 8 digit group with a possible dash in the middle and possible X at the end.
The code above works for most cases, but if capture group 1 is the following example: 123456789 then it also returns positive because it matches the first 8 digits, and I don't want it to.
I tried surrounding capture group 1 with ^...$ but that doesn't work either.
So I would like to match exactly these examples and similar ones:
1234-5678
1234-567X
12345678
1234567X
BUT NOT THESE (and similar):
1234567899
1234567899x
What am I missing?
You may use
^([0-9]{4}-?[Xx0-9]{4})([^0-9].*)?$
See the regex demo
Details
^ - start of string
([0-9]{4}-?[Xx0-9]{4}) - Capturing group 1 ($1): four digits, an optional -, and then four x / X or digits
([^0-9].*)? - an optional Capturing group 2: any char other than a digit and then any 0+ chars as many as possible
$ - end of string.
I need regex that only allows a maximum of 2 digits (or whatever the desired limit is actually) to be entered into an input field.
The requirements for the field are as follows:
Allow a-z A-Z
Allow 0-9
Allow - and . characters
Allow spaces (\s)
Do not allow more than 2 digits
Do not allow any other special characters
I have managed to put together the following regex based on several answers on SO:
^(?:([a-zA-z\d\s\.\-])(?!([a-zA-Z]*\d.*){3}))*$
The above regex is really close. It works successfully for the following:
test 12 test
test12
test-test.12
But it allows an input of:
123 (but not 1234, so it's close).
It only needs to allow an input of 12 when only digits are entered into the field.
I would like some help in finding a more efficient and cleaner (if possible) solution than my current regex - but it must still be regex, no JS.
You could use a positive lookahead like
(?=^(?:\D*\d\D*){2}$) # only two digits
^[- .\w]+$ # allowed characters
See a demo on regex101.com.
You may use a negative lookahead anchored at the start that will make the match fail once there are 3 digits found anywhere in the string:
^(?!(?:[^0-9]*[0-9]){3})[a-zA-Z0-9\s.-]*$
^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?!(?:[^0-9]*[0-9]){3}) - the negative lookahead failing the match if exactly 3 following sequences are found:
[^0-9]* - zero or more chars other than digits
[0-9] - a digit (thus, the digits do not have to be adjoining)
[a-zA-Z0-9\s.-]* - 0+ ASCII letters, digits, whitespace, . or - symbols
$ - end of string.