preg_match allow letters and numbers but at least 1 letter - regex

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.

Related

Regex to match a string containing 14 digits and 1 character at any position

I need a regular expression that matches a string of 15 characters where 14 of them are digits and 1 is a character. The character can be in any position of the string.
I have the following long regex:
^.\d{14}|\d{1}.\d{13}|\d{2}.\d{12}|\d{3}.\d{11}|\d{4}.\d{10}|\d{5}.\d{9}|\d{6}.\d{8}|\d{7}.\d{7}|\d{8}.\d{6}|\d{9}.\d{5}|\d{10}.\d{4}|\d{11}.\d{3}|\d{12}.\d{2}|\d{13}.\d{1}|\d{14}.$
Can it be simplified?
Here is a sample match: 1000-1234567890
(?=^.{15}$)\d{0,14}\D\d{0,14}$
First check the string is 15 characters long, then has 0-14 digits, one non-digit, then 0-14 digits.
This isn't exactly the same as the original regex, which allows 15 digits in a row. To get that, simply change \D to .
We can use a lookaround trick here:
^(?!(?:.*\D){2})(?=.{15}$)\d*\D\d*$
This regex pattern says to match:
^ from the start of the string
(?!(?:.*\D){2}) assert that 2 or more non digits do NOT occur (implying at most 1 occurs)
(?=.{15}$) assert length is 15 characaters
\d*\D\d* then a non digit surrounded, possibly, on either side by numbers
$ end of the string

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

How to validate pattern AB12CD

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

Regex for Password must contain 8 characters, 2 letter both lower or uppercase letters and one special character ' * ' 5 digit number

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}$.

A regular expression that matches 25 chars and starts with digits

I have a text field which I need to validate using a regex. My requirement is as follow:
CCCCNNNNNN or CCCCNNNNNNN (Template)
1234ABCDEFG or 123-ABCDEFG (Example string)
Rules:
The whole string is maximum 25 characters
The first four characters (CCCC) must be alphanumeric
CCCC is 4 characters exactly and can be digits or number
CCCC can have a dash sign as 4th character
NNNNNNNNNNNN can be up to 21 characters and only numbers
E.g. AAAA 1234 A58- is a valid string for CCCC.
Here is my research notes:
I will need to match numerics first
I will need the + char to specify to match this pattern X times
I will need to match letters after that for 8-9 spaces
There is a wonderful post on RegEx patterns here:
Matching numbers with regular expressions — only digits and commas
My goal is to apply this REGEX pattern to a text box Mask in a WinForms app.
....
....
...yeah - I think the answer you are looking for (and I stress "think") is this expression:
^[0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}$
thats:
^ # assert beginning (not in the middle)
[0-9A-Za-z]{3} # three characters that are: 0-9 or a-z (upper or lower)
[0-9A-Za-z-] # one character that is: 0-9 or a-z (upper or lower) or a dash
\d{0,21} # anywhere from 0 to 21 digits
$ # assert at the end (not somewhere in the middle
If you want to match several cases of this expression, put the above expression (minus the assertions) into parantheses (()) along with whatever is allowed to separate these values - I chose \s or "whitespace") and then use the + quantifier:
^([0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}\s+)+$
will match/validate the following input:
1234567890 AAAA123456789012345678901 GGG-123 hhh5 A1B2000000000
If you wanted something else, you'll have to ask a clearer question (there's a lot of contradiction and repetition in your question that makes it EXTREMELY confusing)