RegEx minimum 4 characters with no repetition - regex

Trying to create a regex where a field should contain minimum 4 characters(only alphabets [a-zA-Z]) where
first 4 alphabets should not repeat. eg aaaa,zzzz not acceptable
first 4 characters should not contain space, numbers, special characters
afterwhich anything is fine
I tried following expression but 1 case is failing which is (a123,a##!):
^(?=.{1,4}$)(([a-zA-Z]){1,4}\2?(?!\2))+[a-zA-Z0-9!##$&()\-`.+,"]

You might write the pattern as:
^(?!(.)\1{3})[a-zA-Z]{4}.*
Explanantion
^ Start of string
(?!(.)\1{3}) Negative lookahead, assert not 4 of the same characters
[a-zA-Z]{4} Match 4 chars a-z A-Z
.* Match the rest of the line
Regex demo

Related

Regular expression to validate the given input which accepts one space or Hyphen which is part of the text length it has minimum and maximum length

The following regex working as expected other than the case that it's not allowed that all characters are the same characters.
^(?=[A-Z0-9]+[ -]?[A-Z0-9]+)(?!([A-Z0-9])(?:\1|[ -]){5,10}).{5,10}$
here minimum is 5 characters and the maximum is 10 characters
11114 allowed its minimum length matched as 5 and one charcter is diff so not all same charcters
11111115 allowed as one charcter is different and its more than 5 charcter.
2222222 not allowed as all are same characters
222-22 not allowed as all are same charcters
111-3 allowed as length 5 and one character is different
444-45 allowed as length more than 5
1234565436 allowed as length with in range 5 to 10
There is no need to repeat range quantifier {5,10} multiple times as that makes changing this regex harder for other cases.
You may use this regex for this:
^(?=.{5,10}$)([A-Z0-9])(?!(?:[ -]?\1)+$)[A-Z0-9]*[ -]?[A-Z0-9]+$
RegEx Demo
RegEx Breakup:
^: Start
(?=.{5,10}$): Assert that we have 5 to 10 chars till end
([A-Z0-9]): Match a letter or digit and capture in group #1
(?!(?:[ -]?\1)+$): Negative lookahead to fail the match if same captured value is repeated till end
[A-Z0-9]*: Match 0 or more letter or digit
[ -]?: Match optional space or hyphen
[A-Z0-9]+: Match 1 or more letter or digit
$: End

Reg Expression: GB Vat Number with spaces

I currently have the following regular expression:
^GB([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3})$
This works fine for:
GB999999973
GBGD001
GBHA599
As can be tested here: https://regex101.com/r/jU980W/1
However the problem is that it does not validate with:
GB999 9999 73
I tried adding space indicators to the regular expression but then the other formats aren't supported anymore.
Does anyone know a way to have this regular expression both accept with and without spaces for the GB VAT Number?
Thanks in advance!
See regex in use here
^GB(?:\d{3} ?\d{4} ?\d{2}(?:\d{3})?|[A-Z]{2}\d{3})$
^ Assert position at the start of the line
GB Match this literally
(?:\d{3} ?\d{4} ?\d{2}(?:\d{3})?|[A-Z]{2}\d{3}) Match either of the following options
\d{3} ?\d{4} ?\d{2}(?:\d{3})? Option 1:
\d{3} Match exactly 3 digits
? Optionally match a space
\d{4} Match exactly 4 digits
? Optionally match a space
\d{3} Match exactly 2 digits
(?:\d{3})? Optionally match exactly 3 digits
[A-Z]{2}\d{3} Option 2:
[A-Z] Match any uppercase ASCII letter
\d{3} Match exactly 3 digits
$ Assert position at the end of the line

Regular expression - starting with 3 alphanumeric characters which includes at least one letter and one number, and ending with a letter

I'm trying to make a regex that matches the following criteria:
4 characters.
The beginning 3 characters must be alphanumeric characters, including at least one letter and one digit.
The last character must be a letter.
So I expect the results would be:
case1: abcd -> no match
case2: 234d -> no match
case3: a23c -> match
case4: 3abc -> match
case5: xy23 -> no match
I tested the following regex which matches criteria 2, but still cannot find a solution to match criteria 1&3.
^(?!.*[^a-zA-Z0-9])(?=.*\d)(?=.*[a-zA-Z]).{3}$
I tried this one but it failed on case2.
^(?!.*[^a-zA-Z0-9])(?=.*\d)(?=.*[a-zA-Z]).{3}[a-zA-Z]$
How can I combine these criteria? Thanks!
You may use
^(?=.{0,2}[0-9])(?=.{0,2}[a-zA-Z])[0-9a-zA-Z]{3}[a-zA-Z]$
See the regex demo
Details
^ - start of string
(?=.{0,2}[0-9]) - there must be an ASCII digit after 0 to 2 chars
(?=.{0,2}[a-zA-Z])- there must be an ASCII letter after 0 to 2 chars
[0-9a-zA-Z]{3} - 3 ASCII alphanumerics
[a-zA-Z] - an ASCII letter
$ - end of string
No need to use complicated features for 3 or 4 characters:
/^(?:[a-z0-9](?:[0-9][a-z]|[a-z][0-9])|[0-9][a-z]{2}|[a-z][0-9]{2})[a-z]$/i
or
/^(?:[a-z](?:[0-9][a-z0-9]|[a-z][0-9])|[0-9](?:[a-z][a-z0-9]|[0-9][a-z]))[a-z]$/i

vba regular expression last occurrence

I would like to match the "775" (representing the last 3 digit number with an unkown total number of occurrences) within the string "one 234 two 449 three 775 f4our" , with "f4our" representing an unknown number of characters (letters, digits, spaces, but not 3 or more digits in a row).
I came up with the regular expression "(\d{3}).*?$" thinking the "?" would suffice to get the 775 instead of the 234, but this doesn't seem to work.
Is there any way to accomplish this using VBA regular expressions?
Note that (\d{3}).*?$ just matches and captures into Group 1 the first 3 consecutive digits and then matches any 0+ characters other than a newline up to the end of the string.
You need to get the 3 digit chunk at the end of the string that is not followed with a 3-digit chunk anywhere after it.
You may use a negative lookahead (?!.*\d{3}) to impose a restriction on the match:
\d{3}(?!.*\d{3})
See the regex demo. Or - if the 3 digits are to be matched as whole word:
\b\d{3}\b(?!.*\b\d{3}\b)
See another demo

How to regex a string - length 8, first character letter and remaining numeric

I am trying to create a RegEx to match a string with the following criterion
Length 8
First character must be a letter a-z or A-Z
The remaining 7 must be numeric 0-9
examples
a5554444
B9999999
c0999999
This is what I have so far
^[0-9]{8}$
What am I missing to check the first character? I tried
^[a-zA-Z][0-9]{8}$
but that's not working.
I think this is what you want:
^[a-zA-Z][0-9]{7}$
the {...} metacharacter only matches the most previous pattern which in your case is [0-9]. the regex interpretation is as follows:
start at the beginning of the string (^)
match any character a-z or A-Z ([a-zA-Z]) in the first spot only one time
match any character 0-9 starting at the second spot ([0-9])
the preceding pattern mentioned in step 3 of [0-9] must exist exactly 7 times ({7})
When you put {8} as per your original question, you'll assume a string length total of 9: the first character being alphabetic case insensitive and the remaining 8 characters being numeric.