How to validate pattern AB12CD - regex

The requirement is "each 2 digits must be only numbers or only text" - so valid patterns are AB-12-CD or 12-AB-CD or AB-CD-12, 12-34-AB.
The below suggested pattern working fine without hyphen but if we want to add a hyphen in between, how to do?
\b(?=[A-Z\d][A-Z])(?=[A-Z\d]\d)(?:[A-Z]{2}|\d{2})+\b

Repeat 1 or more times matching either 2 uppercase chars or 2 digits. Note that there are no hyphens present in the example data.
\b(?:[A-Z]{2}|\d{2})+\b
Regex demo
If there must be a digit and an uppercase character present, you could also use a positive lookahead:
\b(?=[A-Z\d]*[A-Z])(?=[A-Z\d]*\d)(?:[A-Z]{2}|\d{2})+\b
Regex demo

Related

Regular Expression to Validate Monaco Number Plates

I would like to have an expression to validate the plates of monaco.
They are written as follows:
A123
123A
1234
I started by doing:
^[a-zA-Z0-9]{1}?[0-9]{2}?[a-zA-Z0-9]{1}$
But the case A12A which is false is possible with that.
You can use
^(?!(?:\d*[a-zA-Z]){2})[a-zA-Z\d]{4}$
See the regex demo. Details:
^ - start of string
(?!(?:\d*[a-zA-Z]){2}) - a negative lookahead that fails the match if there are two occurrences of any zero or more digits followed with two ASCII letters immediately to the right of the current location
[a-zA-Z\d]{4} - four alphanumeric chars
$ - end of string.
You can write the pattern using 3 alternatives specifying all the allowed variations for the example data:
^(?:[a-zA-Z][0-9]{3}|[0-9]{3}[a-zA-Z]|[0-9]{4})$
See a regex demo.
Note that you can omit {1} and
To not match 2 chars A-Z you can write the alternation as:
^(?:[a-zA-Z]\d{3}|\d{3}[a-zA-Z\d]|\d[a-zA-Z\d][a-zA-Z\d]\d)$
See another regex demo.
So it needs 3 connected digits and 1 letter or digit.
Then you can use this pattern :
^(?=.?[0-9]{3})[A-Za-z0-9]{4}$
The lookahead (?=.?[0-9]{3}) asserts the 3 connected digits.
Test on Regex101 here

Match only two combinations and ignore the rest in REGEX - tableau

I have a dozen input ID's and I need to match only two particular patterns while ignoring the rest. I have a column that would flag those valid/invalid if the regex match is true.
Test string:
1.) B-123456
2.) 985463728
My regex should strictly match the above two patterns and ignore the rest. The first test string would have an alphabet B followed by a hyphen and then few digits while the second test string is purely numbers. Below is what I tried:
[Bb\d][-\d][0-9]{1,9}
Please help me out with this as I have tried weird combinations and I am missing out on something tiny. My regex includes other combinations as well which should not happen.
You could match either bB a - and 6 digits, or match 9 digits surrounded by word boundaries:
\b(?:[Bb]-[0-9]{6}|[0-9]{9})\b
Regex demo
If the number of digits can vary, you could make the bB and the hyphen optional and either match 1+ digits using [0-9]+ or use a quantifier [0-9]{1,9}
\b(?:[bB]-)?[0-9]+\b
Or use anchors to assert the start ^ and the end $ of the string
^(?:[bB]-)?[0-9]+$

Only allow 2 digits in a string using regex

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.

Regular expression of two digit number where two digits are not same

I am trying to write a regular expression that will match a two digit number where the two digits are not same.
I have used the following expression:
^([0-9])(?!\1)$
However, both the strings "11" and "12" are not matching. I thought "12" would match. Can anyone please tell me where I am going wrong?
You need to allow matching 2 digits. Your regex ^([0-9])(?!\1)$ only allows 1 digit string. Note that a lookahead does not consume characters, it only checks for presence or absence of something after the current position.
Use
^(\d)(?!\1)\d$
^^
See demo
Explanation of the pattern:
^ - start of string
(\d) - match and capture into Group #1 a digit
(?!\1) - make sure the next character is not the same digit as in Group 1
\d - one digit
$ - end of string.

A special Regular Expression

I want to have a restriction a string which can accept alphanumeric values and hiphen.
I am providing 3 examples to have a clear idea.
1) AS15JKM-125TR-325AMOR
2) ITEW32-DE432OI
3) 09IURE765EDR
There is no specific pattern, There may b 0 to 3 hiphens in a string.
I just want to restrict it in such a way that it should accept only alphanumeric value and
only Hiphen, no other special character.
plz help me on this.
Option 1: No Lookahead
^(?:[A-Za-z0-9]*-){0,3}[A-Za-z0-9]+$
Note that if you only want uppercase letters, you need to remove a-z
Explanation
The ^ anchor asserts that we are at the beginning of the string
The non-capturing group (?:[A-Za-z0-9]*-) matches zero or more letters or digit, then a hyphen
This is repeated zero to three times, enforcing your limit on hyphens
[A-Za-z0-9]+ matches one or more letters or digit
The $ anchor asserts that we are at the end of the string
Option 2: With Lookahead
This does not present any benefit over the first version, I am just showing it for completion.
^(?=(?:[^-]*-){0,3}[^-]*$)[A-Za-z0-9]+$
Explanation
The lookahead (?=(?:[^-]*-){0,3}[^-]*$) asserts that what follows is
(?:[^-]*-) any number of non-hyphens, followed by a hyphen
{0,3} zero to three times
then [^-]*$ any number of non-hyphens and the end of the string
Option 3: With Negative Lookahead
Courtesy of #Jerry:
^(?!(?:[^-]*-){4})[A-Za-z0-9]+$
Explanation
The negative lookahead (?!(?:[^-]*-){4}) asserts that it is not possible to find a non-hyphen followed by a hyphen four times.
Assuming you do not want to count the hyphens, something like so should work: ^[A-Z0-9 -]+$.
An example of the regex is available here.