I thought this regular expression would work:
preg_match("/^e[a-z]i\d{7}$/", $_POST['username'])
I'm trying to validate a user's login credentials to match the following format:
e00123456
The 'e' can be either e or E and the 8 numbers after the 'e' can be any number ranging from 0-9. But it has to be the letter 'e' followed by 8 numbers, no more, no less. What's wrong with my regular expression?
you're currently matching:
^: From the start
e: find an e
[a-z]: then one character which is in abcdefghijklmnopqrstuvwxzy
i: followed by an i
\d{7}: followed by seven digits
$: And only match if this is the last character.
You're not yet being case-insensitive.
You want to do:
/^e\d{8}$/i
You will probably need to escape the \. The /i sets case-insensitive mode in sed and Vim, you might need to set that differently if you're using PHP.
What's the point of [a-z]? That will match any lower case letter. Also, why do you have \d{7} if you want 8 digits? Try this:
^[eE]\d{8}$
The [eE] character class will match either an upper or lower case 'e'.
Use may use this in your regular expression validation
[eE]{1}\d{8}
[eE] means only 'e' or 'E' is allowed
{1} means only 1st letter
\d{8} means next 8 letters will be digits ..
Hope, this may help you .
Related
I have the following preg_match statement that allows an input to be either letters a to z and numbers 0 to 9 only and it must be between 5 and 30 characters in length.
However, how would I also make sure that the input is not all numerics? For example it must contain at least 1 a to z letter?
if (preg_match ('/^[a-zA-Z0-9\'.]{5,30}$/i', $str['user'])) {
}
You wrote you have a regex that allows an input to be either letters a to z and numbers 0 to 9 only, but actually ^[a-zA-Z0-9\'.]{5,30}$ matches a string of 5 to 30 ASCII letters, digits and also ' or ..
If you just want to make your regex match what it matches, but not a string that only consists of digits, use a (?!\d+$) lookahead:
'/^(?!\d+$)[a-z0-9\'.]{5,30}$/i'
^^^^^^^^
Note you do not need to specify the A-Z or a-z range if you use a i case insensitive flag (just either of them is enough).
The (?!\d+$) is a negative lookahead that fails the match if there are one or more digits followed by the end of string ($) immediately to the right of the current location (which is the start of the string).
See the regex demo.
If you do not want to match a string that only consists of dots, or ', or digits, use
'/^(?!(?:\d+|([\'.])\1*)$)[a-z0-9\'.]{5,30}$/i'
See another demo.
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.
My RegExp is very rusty! I have two questions, related to the following RegExp
Question Part 1
I'm trying to get the following RegExp to work
^.*\d{1}\.{1}\d{1}[A-Z]{5}.*$
What I'm trying to pass is x1.1SMITHx or x1.1.JONESx
Where x can be anything of any length but the SMITH or JONES part of the input string is checked for 5 upper case characters only
So:
some preamble 1.1SMITH some more characters 123
xyz1.1JONES some more characters 123
both pass
But
another bit of string1.1SMITHABC some more characters 123
xyz1.1ME some more characters 123
Should not pass because SMITH now contains 3 additional characters, ABC, and ME is only 2 characters.
I only pass if after 1.1 there are 5 characters only
Question Part 2
How do I match on specific number of digits ?
Not bothered what they are, it's the number of them that I can't get working
if I use ^\d{1}$ I'd have thought it'll only pass if one digit is present
It will pass 5 but it also passes 67
It should fail 67 as it's two digits in length.
The RegExp should pass only if 1 digit is present.
For the first one, check out this regex:
^.*\d\.\d[A-Z]{5}[^A-Z]*$
Before solving the problem, I made it easier to read by removing all of the {1}. This is an unnecessary qualifier since regex will default to looking for one character (/abc/ matches abc not aaabbbccc).
To fix the issue, we just need to replace your final .*. This says match 0+ characters of anything. If we make this "dot-match-all" more specific (i.e. [^A-Z]), you won't match SMITHABC.
I came up with a number of solution but I like these most. If your RegEx engine supports negative look-ahead and negative look-behind, you can use this:
Part 1: (?<![A-Z])[A-Z]{5}(?![A-Z])
Part 2: (?<!\d)\d(?!\d)
Both have a pattern of (?<!expr)expr(?!expr).
(?<!...) is a negative look-behind, meaning the match isn't preceded by the expression in the bracket.
(?!...) is a negative look-ahead, meaning the match isn't followed by the expression in the bracket.
So: for the first pattern, it means "find 5 uppercase characters that are neither preceded nor followed by another uppercase character". In other words, match exactly 5 uppercase characters.
The second pattern works the same way: find a digit that is not preceded or followed by another digit.
You can try it on Regex 101.
I want to create an expression for password as below:
Regex for passwords that must contain 8 characters, start with 2 lower or uppercase letters, contain one special character * and a 5-digit number.
E.g.: az*12345
It must be start with 2 characters;
Contain only single *;
End with 5 digits.
I have tried it with this pattern:
(?=(.*[^a-zA-Z]){2})(?=.*[*]{1})(?=(.*\d){5}).{8}$
However, it yields almost the same results as a regex above. It starts with any character but I want the exact above mentioned pattern. I know I am close to it. Please suggest me what I should do.
If you wish to match just [2-letters]+[*]+[5-digits] pattern, here is what you are looking for:
^[a-zA-Z]{2}\*[0-9]{5}$.
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