Regular expression to validate a specific pattern - regex

I'm trying to create a regular expression with the below restrictions
Allow any character '\w\W\s' (e.g rand123#!##adfads)
Disallow only numbers (e.g 12312312)
Disallow only non alphanumeric characters (e.g !##$$#%#^%$^%)
Number of characters must be between 3 to 60
Following along the lines of this answer, but could not get it work.
^(?=.{3,60}$)(?![\W_]*$)(?![0-9]*$)[\w\W\s]+$

Note that \W matches \s, so '\w\W\s' can be reduced to [\w\W].
You may use 2 negative lookaheads anchored at the start to impose two "disallow-only" conditions like this:
^(?![\W_]+$)(?![0-9]+$)[\w\W]{3,60}$
See the regex demo
Pattern details:
^ - start of string
(?![\W_]+$) - the string cannot consist of non-alphanumeric chars
(?![0-9]+$) - the string cannot consist of digits only
[\w\W]{3,60} - 3 to 60 any characters
$ - end of string.

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.

Regular Expression to Validate Monaco Number Plates

I would like to have an expression to validate the plates of monaco.
They are written as follows:
A123
123A
1234
I started by doing:
^[a-zA-Z0-9]{1}?[0-9]{2}?[a-zA-Z0-9]{1}$
But the case A12A which is false is possible with that.
You can use
^(?!(?:\d*[a-zA-Z]){2})[a-zA-Z\d]{4}$
See the regex demo. Details:
^ - start of string
(?!(?:\d*[a-zA-Z]){2}) - a negative lookahead that fails the match if there are two occurrences of any zero or more digits followed with two ASCII letters immediately to the right of the current location
[a-zA-Z\d]{4} - four alphanumeric chars
$ - end of string.
You can write the pattern using 3 alternatives specifying all the allowed variations for the example data:
^(?:[a-zA-Z][0-9]{3}|[0-9]{3}[a-zA-Z]|[0-9]{4})$
See a regex demo.
Note that you can omit {1} and
To not match 2 chars A-Z you can write the alternation as:
^(?:[a-zA-Z]\d{3}|\d{3}[a-zA-Z\d]|\d[a-zA-Z\d][a-zA-Z\d]\d)$
See another regex demo.
So it needs 3 connected digits and 1 letter or digit.
Then you can use this pattern :
^(?=.?[0-9]{3})[A-Za-z0-9]{4}$
The lookahead (?=.?[0-9]{3}) asserts the 3 connected digits.
Test on Regex101 here

Regex - Allow Alphanumeric, spaces and symbols but CANNOT be only numeric or symbols or spaces. Must have alphabets in it

How can I write this rules using regex :
Allow :
- Alphanumeric
- Spaces
- Symbols
Required :
Alphabetical
A minimum of two of the allowed
For example :
"abc123--" is accepted string
"abc" is rejected
"123-9*" is rejected
The comment that now you have 2 problems was a bit malevolent.
Regular expressions are just the right solution to verify such things,
under condition that you know how to do it.
A general rule to verify a text for presence / absence of particular chars is:
Start from ^ anchor.
Put a number of positive / negative lookaheads, verifying all criteria but the last.
Put "ordinary" regex expression, trying to match the last criterion.
End with $ anchor.
From what you decribed as acceptable / unacceptable strings, I see that you
have additional requirement: The string must contain al least 1 digit
(because you described abc as unacceptable).
So the regex should contain the following parts:
^ - Start anchor.
(?=(?:.*[a-z]){2,}) - Positive lookup for a letter, after 0 or more any chars
(i.e. somewhere in the string), 2 times or more.
(?=.*\d) - Positive lookup for a digit, after 0 or more any chars.
[\w!##$%^&*+;:,.-]+ - Specification of what you want to
match - "allowed" chars, occurring 1 or more times.
If you need any more punctation characters, just add them here.
Note that - is at the end, otherwise you should have quoted it with
a backslash. Other chars (e.g. ., * and +) need no quotation
between [ and ] (they represent just themselves).
$ - End anchor.
Note that \w covers letters, digits and _.
To sum up, the whole regex is:
^(?=(?:.*[a-z]){2,})(?=.*\d)[\w!##$%^&*+;:,.-]+$
Of course, use it with i (case insensitive) option.

Allowing any string except the one containing specific format of numbers using regular expression

My Requirement:
I have to allow any string input in a textBox but it should not contain a 10 digit number in either of following formats:
dddddddddd
OR
dddddd-dddd
The regular expression which I have used is I have used is
^((?!\d{10})(?!\d{6}-\d{4}).)*$
It works fine but does not allow an input of more than 10 digits also.
The ((?!\d{10})(?!\d{6}-\d{4}).)* is a tempered greedy token that matches any char, 0 or more times, that does not start a 10-digit or 6-digit+-+4-digit char sequences. It "disallows" a string to contain these patterns.
You can use
^(?!\d{6}-?\d{4}$).*$
See the regex demo.
Details
^ - start of the string
(?!\d{6}-?\d{4}$) - a negative lookahead that fails the match if the whole string is either 10 digits, with an optional - between the 6th and the 7th digit
.*$ - the rest of the string.
NOTE: you do not need $ at the end, actually, but you may keep it for additional clarity. .* matches to the end of the line by default, and if there can be no line breaks, the $ is totally redundant.

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.