A regular expression that matches 25 chars and starts with digits - regex

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)

Related

Regex for a promo code that has the following rules

I need to build a regex that have the following:
Rules to be applied:
exactly 14 characters
only letters (latin characters) and numbers
at least 3 letters
Regex still confuses me so I am struggling to get the correct output. I want to use it with swift and swiftui in an app I am making
(?=(.*[a-zA-Z]){3,}([0-9]){0,}){14,14}$
I tried this. But I know it is not the way
I would use a positive lookahead for the length requirement:
^(?=.{14}$)(?:[A-Za-z0-9]*[A-Za-z]){3}[A-Za-z0-9]*$
This pattern says to match:
^ from the start of the input
(?=.{14}$) assert exact length of 14
(?:
[A-Za-z0-9]*[A-Za-z] zero or more alphanumeric followed by one alpha
)
[A-Za-z0-9]* any alphanumeric zero or more times
$ end of the input
You need to use
^(?=(?:[0-9]*[a-zA-Z]){3})[a-zA-Z0-9]{14}$
Details
^ - start of string
(?=(?:[0-9]*[a-zA-Z]){3}) - at least three repeations of a letter after any zero or more digits sequence required
[a-zA-Z0-9]{14} - fourteen letters/digits
$ - end of string.
See the regex demo.

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,}

Only allow 2 digits in a string using regex

I need regex that only allows a maximum of 2 digits (or whatever the desired limit is actually) to be entered into an input field.
The requirements for the field are as follows:
Allow a-z A-Z
Allow 0-9
Allow - and . characters
Allow spaces (\s)
Do not allow more than 2 digits
Do not allow any other special characters
I have managed to put together the following regex based on several answers on SO:
^(?:([a-zA-z\d\s\.\-])(?!([a-zA-Z]*\d.*){3}))*$
The above regex is really close. It works successfully for the following:
test 12 test
test12
test-test.12
But it allows an input of:
123 (but not 1234, so it's close).
It only needs to allow an input of 12 when only digits are entered into the field.
I would like some help in finding a more efficient and cleaner (if possible) solution than my current regex - but it must still be regex, no JS.
You could use a positive lookahead like
(?=^(?:\D*\d\D*){2}$) # only two digits
^[- .\w]+$ # allowed characters
See a demo on regex101.com.
You may use a negative lookahead anchored at the start that will make the match fail once there are 3 digits found anywhere in the string:
^(?!(?:[^0-9]*[0-9]){3})[a-zA-Z0-9\s.-]*$
^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?!(?:[^0-9]*[0-9]){3}) - the negative lookahead failing the match if exactly 3 following sequences are found:
[^0-9]* - zero or more chars other than digits
[0-9] - a digit (thus, the digits do not have to be adjoining)
[a-zA-Z0-9\s.-]* - 0+ ASCII letters, digits, whitespace, . or - symbols
$ - end of string.

Need a Regex that contains at least one number, zero or more letters, no spaces, min/max

I need a regular expression that will match a string that contains:
at least one number
zero or more letters
no other characters such as spaces
The string must also be a minimum of 8 characters and a maximum of 13 characters.
Placement of the numbers and/or letters within the 8-13 character string does not matter. I haven't figured out how to make sure that the string contains a number, but here are some expressions that don't work because they are picking up spaces in the online tool Regexr. Take a look below:
- ([\w^/s]){8,13}
- ([a-zA-Z0-9]){8,13}
- ([a-zA-Z\d]){8,13}
I am specifically looking to exclude spaces and special characters. The linked and related questions all appear to allow for these characters. This is not for validating passwords, it is for detecting case numbers in natural language processing. This is different from "Password REGEX with min 6 chars, at least one letter and one number and may contain special characters" because I am looking for at least one number but zero or more letters. I also do not want to return strings that contain any special characters including spaces.
This is a typical password validation with your requirements.
Note that this will also match 8-13 digits as well (but it is requested).
Ten million + 1 (and counting) happy customers ..
^(?=.*\d)[a-zA-Z\d]{8,13}$
Explained
^ # Beginning of string
(?= .* \d ) # Lookahead for a digit
[a-zA-Z\d]{8,13} # Consume 8 to 13 alphanum characters
$ # End of string
I've seen the answer above (by sln) everywhere over the internet, but as far as I can tell, it is NOT ACCURATE.
If your string contains 8 to 13 characters with no numbers this expression will match it, because it uses the * quantifier on the wildcard character . in the positive lookahead.
In order to match at least 1 digit, 1 A-Z and 1 a-z in a password that's at least 8 characters long, you'll need something like this:
(?=.{1,7}\d)(?=.{1,7}[a-z])(?=.{1,7}[A-Z])[a-zA-Z\d]{8,13}
it uses 3 lookaheads:
(?=.{1,7}\d)
(?=.{1,7}[a-z])
(?=.{1,7}[A-Z])
each time, it looks for the target (eg the first digit) but allows 1 to 7 occurances of any character before it.
Then it will match 8 to 13 alphanumeric characters.
NOTE to Powershell users:
Use a search group to be able to extract a result
$password = [regex]::match($string-to-search,'(?=.{1,7}\d)(?=.{1,7}[a-z])(?=.{1,7}[A-Z])([a-zA-Z\d]{8,13})').Groups[1].Value

Reg Exp: match specific number of characters or digits

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.