Check uppercase AND lowercase - regex

I'm searching from a while for the regular expression that matches a word containing between 8 and 20 characters combining uppercases AND lowercases:
Here an example of valid expression : AaAaAaAaA or aaaaaaaaA
Not Valid expression example : aaaaaaaaa or AAAAAAAA
What I did till now is : ^[a-z][A-Z]{8,20}$, but as you can see, that doesn't work
Somebody have an idea for that ?
Best regards

Credit to anubhava, lookahead was the way to go.
\b(?=[a-z]+[A-Z]+|[A-Z]+[a-z]+)[a-zA-Z]{8,20}\b
The look ahead ensures that there is at least one capital and lowercase letter in any order in the match. This will match whole words that are 8-20 chars long and contain at least 1 upper case and at least 1 lower case letter.
^(?=[a-z]+[A-Z]+|[A-Z]+[a-z]+)[a-zA-Z]{8,20}$
Will anchor to the beginning and end of the string, and thus match only a single word.
You can see it in action here http://regex101.com/r/uE5lT4/4
Edit: First version did not match a word if the only capital was the last letter.

Your character class needs to include both a-z and A-Z ranges. Regex should be:
^[a-zA-Z]{8,20}$
Your regex:
^[a-z][A-Z]{8,20}$
is actually checking for a lower case character followed by 8 to 20 upper case alphabets.
Update: to make sure you match at least one lower and one upper case alphabet use lookahead regex like this:
^(?=[A-Z]*[a-z])(?=[a-z]*[A-Z])[a-zA-Z]{8,20}$
RegEx Demo

Related

regex few blocks exact length

I want to match 5 to 20 character with regex.
I try to use below regular expression for my checking.
/^[a-zA-Z][\w]{5,20}$/
It's work, but the problem of length it match 6 to 21 character.
(^[a-zA-Z][\w]){4,20}$
I also try this but it don't work.
Please anyone help me to match exact length of regex.
It's because your capturing group is expecting TWO characters:
[a-zA-Z] and [\w], that's two letters.
So your first attempt actually did this:
match [a-zA-Z] once
match [\w] once
match the previous matches 5 - 20 times
Inevitably, you always had 1 more match than expected
Capture only one character, and iterate it 5-20 times.
Have you tried:
^([a-zA-Z]{5,20})$ ?
OR
^(\w{5,20})$ ?
You're almost there, you just need to make a single range of characters (in square brackets) not two.
/^[a-zA-Z][\w]{5,20}$/ means:
a character from a to z in lower or upper case
5 to 20 word characters
That sums up to 6 to 21 characters in total.
I suppose you want /^[a-zA-Z][\w]{4,19}$/:
a character from a to z in lower or upper case
4 to 19 word characters
That sums up to 5 to 20 characters in total.
The Quantifier is only applied to the [\w]. So this expects exactly one letter character and then 5-20 whitespace characters.
I assume you want 5-20 characters that can be either a letter a-z or a whitespace. You need to group these together in square brackets and then apply the quantifier:
^[a-zA-Z\W]{5,20}$
So, I understand, you want a string that has 5-20 characters, starts with a letter and then only has letters and digits. You would write it like that:
^[a-zA-Z][a-zA-Z0-9]{4,19}$
This expects first a letter and then 4-19 letters or digits.
BTW: https://regex101.com/ is a great site to test regular expressions and get an explanation what they are doing.

Regex: Match if strings starts with a letter or number and then

I'm wanting to match a string if begins with either a letter or number, and from there I want to count the string (excluding whitespaces), and if it's over 5 characters, match it.
I believe I'm pretty close, my current regex is:
\s*(?:\S[\t ]*){5,}
What I need to add, is making sure the string starts with either a letter or number (or if it begins with a whitespace, make sure the following character is a letter or number.)
http://regex101.com/r/lD7mZ2/1
How about the regex
^\s*[a-zA-Z0-9]\s*(?:\S[\t ]*){4,}
Example: http://regex101.com/r/lD7mZ2/4
Changes made
^ anchors the regex at the start of the string.
[a-zA-Z0-9] matches letter or digit
{4,} quantifies it minimum 4 times. The presceding \w makes length of minimum 5
OR
a shorter version would be
^\s*[a-zA-Z0-9]\s*(?:\S\s*){4,}

Regular expression for first 4 characters

I need a regular expression for 4 characters. The first 3 characters must be a number and the last 1 must be a letter or a digit.
I formed this one, but it not working
^([0-9]{3}+(([a-zA-Z]*)|([0-9]*)))?$
Some valid matches: 889A, 777B, 8883
I need a regular expression for first 3 will be a number and the last 1 will be a alphabet or digit
This regex should work:
^[0-9]{3}[a-zA-Z0-9]$
This assumes string is only 4 characters in length. If that is not the case remove end of line anchor $ and use:
^[0-9]{3}[a-zA-Z0-9]
Try this
This will match it anywhere.
\d{3}[a-zA-Z0-9]
This will match only beginning of a string
^\d{3}[a-zA-Z0-9]
You can also try this website: http://gskinner.com/RegExr/
It makes it very easy to create and test your regex.
Just take the stars out...
^([0-9]{3}+(([a-zA-Z])|([0-9])))?$
The stars mean zero or more of something before it. You are already using an or (|) so you want to match exactly one of the class, or one of the other, not zero or more of the class, or zero or more of the other.
Of course, it can be simplified further:
^\d{3}[a-zA-Z\d]$
Which literally means... three digits, followed by a character from either lowercase or uppercase a-z or any digit.

What does this regex does?

This is the string from Automapping configuration of NHibernate. I wonder what it does.
return string.Format("{0}_", Regex.Replace(member.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1_").ToUpper());
Ok, let's break it up.
//This is the start
([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))
[a-z](?=[A-Z]) //this means to match one lower case a-z followed by an uppercase A-Z
| //or
[A-Z](?=[A-Z][a-z]) //One uppercase A-Z followed by one uppercase and one lowercase a-z
//The replace
$1_ //Replace the match with "the match plus underscore".
//aBxx would become a_Bxx and ABcxx would be A_Bcxx
Lets break it down:
([a-z](?=[A-Z])
The first section matches any lower case character that ends in an upper case character
|[A-Z](?=[A-Z][a-z]))
Or any upper case character that ends in an upper then a lower case character
For example this will match
AAb with the first 'A' being the match or
aB
with the 'a' being the match.
The regex is using http://www.regular-expressions.info/lookaround.html

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.