Regex how to identify length of numbers - regex

lets say i have String which starts with two letters and letters are followed with underscore and numbers
XX_333-335 [A-Za-z]{2}_\d{3}-\d{3}`
` - this works good for me
but string can be: (this works as well)
XX_333-335;338;340-341 -^[A-Za-z]{2}_\d{3}-\d{3};|\d{3}|\d{3}-\d{3}
but how to check with regex if number length is 3 places?
XX_0333-0335;0338;340-0341 - In this example there should be no matches, because numbers have four positions not three
Is there any way how to solve it with regex?
thank you

You can use
^[A-Za-z]{2}_\d{3}(?:-\d{3})?(?:;\s*\d{3}(?:-\d{3})?)*$
See the regex demo
Details
^ - start of string
[A-Za-z]{2} - two ASCII letters
_ - an underscore
\d{3} - three digits
(?:-\d{3})? - an optional occurrence of - and three digits
(?:;\s*\d{3}(?:-\d{3})?)* - zero or more occurrences of a semi-colon followed with 0+ whitespaces, three digits and an optional sequence of three digits
$ - end of string.

This would do it:
^[A-Za-z]{2}_(?:\d{3}(?:[; -]+|$))+$
https://regex101.com/r/4ULAql/1

Related

Inputmask - Regex currency and optional decimal places

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.

Regex to block more than 3 numbers in a 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.

combinig regex partly using | not working c++

so this is going to be a regex for email address.
username: capital and small letters, digits, and underscore and dot
^[A-Za-z0-9._]+
then there's # and domain: capital and small letters and digits
#[A-Za-z0-9]+
then there's dot and tld: at least 2 characters (letters and digits) and can have maximum one dot.
I used | to have both at least 2 characters and maximum one dot:
([A-Za-z0-9]{2,}|[.]{0,1})
so the complete regex is this:
regex reg ("^[A-Za-z0-9._]+#[A-Za-z0-9]+\\.([A-Za-z0-9]{2,}|[.]{0,1})$");
but the maximum one dot rule isn't working. when I input zohal#gmail.df.g (not real of course) it gives false. it does work in other cases like zohal#gmail.com though.
You may use
regex reg(R"(^[A-Za-z0-9._]+#[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)+$)")
See the regex demo
Details
^ - start of string
[A-Za-z0-9._]+ - 1 or more letters, digits, . or _
# - a # char
[A-Za-z0-9]+ - 1 or more letters or digits
(?:\.[A-Za-z0-9]+)+ - 1 or more occurrences of a dot and then 1 or more letters or digits
$ - an end of string position.
Since there are two + quantified patterns after #, you do not need an explicit (?=(?:[^A-Za-z0-9]*[A-Za-z0-9]){2}) lookahead to require at least two letters or digits.

Regex: Find last occurrence of digit pair

I'm trying to find the last match of a digit pair in some kinds of strings like these:
123f64swfW68F43
123f64swfW6843
123f64swfW6843sad
123f64swfW6843sa3d
In all of them the matching result should be 43. I tried my best and came to:
/(\d{2})(?!.*\d)/
But this works only for the first three strings.
Please note that I want to do this in one regular expression and without any further scripting.
Thanks for your help!
You may use
\d{2}(?!\d|.*\d{2})
See the regex demo. It basically means "match 2 consecutive digits that are not immediately followed with a digit and that not followed with any 2 consecutive digits anywhere to the right of those two digits".
Details
\d{2} - two digits
(?!\d|.*\d{2}) - that are not followed with a digit or with any two digits aftr any 0+ chars other than line break chars.
Alternatively, you may use
/.*(\d{2})/
and grab Group 1 value. See the regex demo. This regex means "match all text it can to the last two digits, and capture the two digits in a separate memory buffer".
Details
.* - any 0+ chars other than line break chars, as many as possible
(\d{2}) - Capturing group 1: two digits

regex that checks if the string starts with two upper-case followed by numbers

Hi Guys I need a regex that checks if the string starts with two upper-case followed by numbers:
Example: DE123456789
Thanx a lot of.
Literally:
^[A-Z]{2}\d+
^ - start of the string
[A-Z] - an upper case letter
{2} - two of those
\d - a digit
+ - one or more of those