regex few blocks exact length - regex

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.

Related

Regular expression limit length of character while not matching last character of string

I'm working on a new regex to prepare for a host-name on a virtual machine, however, I'm running into issue on how to limit character length of 24 while making sure the last character is not a dot or a minus. (the first character must be an alpha character)
I have gotten as far as making sure the first character is an alpha. The second group of characters are 23 in length with [a-zA-z0-9] including the dot and minus. I've tried the negative look-behind .+(?<!-|\.)$ in addition but does not work.
^[a-zA-Z]([a-zA-Z0-9-.]{0,23}
I expect the output of a123456789012345678911234 to be correct already.
I expect this output should be incorrect a12345678901234567891123-
You may use
^[a-zA-Z](?:[a-zA-Z0-9.-]{0,22}[a-zA-Z0-9])?$
See the regex demo and the regex graph:
Details
^ - start of string
[a-zA-Z] - a letter
(?:[a-zA-Z0-9.-]{0,22}[a-zA-Z0-9])? - an optional sequence of:
[a-zA-Z0-9.-]{0,22} - 0 to 22 letters, digits, . or - chars
[a-zA-Z0-9] - a letter or digit
$ - end of string.
In order to limit the number of characters, the expression must be enclosed in ^ and $ denoting the beginning and the end.
^[a-zA-Z][a-zA-Z0-9.-]{0,22}[a-zA-Z0-9]$
[] defines one character from those in parentheses
{a, b} defines the number of occurrences of the preceding character from 0 to 22 in the example
a total limit of 2 to 24 characters
This can be saved shorter but in this way it is the easiest to understand.

Regex for email, between 10 and 30 characters

I have to make regex expression for e-mail.
It is allowed to have letters or numbers only before the # symbol, optionally only the dot that can not be on the start. After # symbol it is allowed to have letters or numbers only, exactly one dot and after dot at least 2 characters.
That's my expression
/([a-zA-Z\d*])+(\.?)([a-zA-Z\d*])*#{1}([a-zA-z\d*])+(\.){1}([a-zA-Z\d*]){2,}/
Whole email should have between 10 and 30 characters, and I don't know how to do that.
Thanks
Actually, you can prefix your pattern with a look-ahead and a quantifier to match between 10 and 30 characters: (?=^.{10,30}$)
Then, your pattern looks like this:
(?=^.{10,30}$)([a-zA-Z\d*])+(\.?)([a-zA-Z\d*])*#{1}([a-zA-z\d*])+(\.){1}([a-zA-Z\d*]){2,}
Demo
The syntax for a range of allowed repeats is {n,m}. You wrote {1} meaning "exactly one" which is pointless. {10,30} is the range you are looking for.
Also, know the escape code for "letters". \w is a "word character", which is the same as [a-zA-Z0-9_]. And why is there a '*' in the character range?
So the problem is that you have pieces which can end up being of various lengths, and need to check the total when done, right?
In Perl you can include code as a assertion. So include (?{ length($&) <= 30 }) as the final assertion.
^(?=^.{10,30}$)(?=^[A-Za-z0-9])[A-Za-z0-9\.]+#[A-Za-z0-9]+\.[A-Za-z]{2,}$
https://regex101.com/r/cBACF2/10
Should be between 10 to 30 (?=^.{10,30}$)
Start with letters & numbers (?=^[A-Za-z0-9])
contains letters & numbers and dot [A-Za-z0-9\.]
at least two letter after after last dot [A-Za-z]{2,}

Regex - matching while ignoring some characters

I am trying to write a regex to max a sequence of numbers that is 5 digits long or over, but I ignore any spaces, dashes, parens, or hashes when doing that analysis. Here's what I have so far.
(\d|\(|\)|\s|#|-){5,}
The problem with this is that this will match any sequence of 5 characters including those characters I want to ignore, so something like "#123 " would match. While I do want to ignore the # and space character, I still need the number itself to be 5 digits or more in order to qualify at a match.
To be clear, these would match:
1-2-3-4-5
123 45
2(134) 5
Bonus points if the matching begins and ends with a number rather than with one of those "special characters" I am excluding.
Any tips for doing this kind of matching?
If I understood requirements right you can use:
^\d(?:[()\s#-]*\d){4,}$
RegEx Demo
It always matches a digit at start. Then it is followed by 4 or more of a non-capturing group i.e. (?:[()\s#-]*\d) which means 0 or more of any listed special character followed by a digit.
So just repeat a digit, followed by any other sequence of allowed characters 5 or more times:
^(\d[()\s#-]*){5,}$
You can ensure it ends on a digit if you subtract one of the repetitions and add an explicit digit at the end:
^(\d[()\s#-]*){4,}\d$
You can suggest non-digits with \D so et would be something like:
(\d\D*){5,}
Here is a guide.

RegEx - 1 to 10 Alphanumeric Spaces Okay

New to Regular Expressions. Thanks in advance!
Need to validate field is 1-10 mixed-case alphanumeric and spaces are allowed. First character must be alphanumeric, not space.
Good Examples:
"Larry King"
"L King1"
"1larryking"
"L"
Bad Example:
" LarryKing"
This is what I have and it does work as long as the data is exactly 10 characters. The problem is that it does not allow less than 10 characters.
[0-9a-zA-Z][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ]
I've read and tried many different things but am just not getting it.
Thank you,
Justin
I don't know what environment you are using and what engine. So I assume PCRE (typically for PHP)
this small regex does exact what you want: ^(?i)(?!\s)[a-z\d ]{1,10}$
What's going on?!
the ^ marks the start of the string (delete it, if the expression must not match the whole string)
the (?i) tells the engine to be case insensitive, so there's no need to write all letter lower and upper case in the expression later
the (?!\s) ensures the following char won't be a white space (\s) (it's a so called negative lookahead)
the [a-z\d ]{1,10} matches any letter (a-z), any digit (\d) and spaces () in a row with min 1 and max 10 occurances ({1,10})
the $ at the end marks the end of the string (delete it, if the expression must not match the whole string)
Here's also a small visualization for better understanding.
Debuggex Demo
Try this: [0-9a-zA-Z][0-9a-zA-Z ]{0,9}
The {x,y} syntax means between x and y times inclusive. {x,} means at least x times.
You want something like this.
[a-zA-Z0-9][a-zA-Z0-9 ]{0,9}
This first part ensures that it is alphanumeric. The second part gets your alphanumeric with a space. the {0,9} allows from anywhere from 0 to 9 occurrences of the second part. This will give your 1-10
Try this: ^[(^\s)a-zA-Z0-9][a-z0-9A-Z ]*
Not a space and alphanumeric for the first character, and then zero or more alphanumeric characters. It won't cap at 10 characters but it will work for any set of 1-10 characters.
The below is probably most semantically correct:
(?=^[0-9a-zA-Z])(?=.*[0-9a-zA-Z]$)^[0-9a-zA-Z ]{1,10}$
It asserts that the first and last characters are alphanumeric and that the entire string is 1 to 10 characters in length (including spaces).
I assume that the space is not allowed at the end too.
^[a-zA-Z0-9](?:[a-zA-Z0-9 ]{0,8}[a-zA-Z0-9])?$
or with posix character classes:
^[[:alnum:]](?:[[:alnum:] ]{0,8}[[:alnum:]])?$
i think the simplest way is to go with \w[\s\w]{0,9}
Note that \w is for [A-Za-z0-9_] so replace it by [A-Za-z0-9] if you don't want _
Note that \s is for any white char so replace it by if you don't want the others

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.