my regex code not working perfectly need assistance - regex

i am trying to remove all lines that its password doesnt follow my standard policy
username:password
the policy are
Passwords are after ":" And password policy is that the password must be 7 to 32 characters long and The password must contain a mix of letters, numbers, and/or special characters also passwords containing only letters or only numbers are not accepted
username:Password42
Username52#:sssdt3
user:Pass!626795
use:uss
removing all lines only leaving
username:Password42
user:Pass!626795
i tried using regex, Made this one but it doesnt work perfectly, Why so? Anyone can fix it?
^:*(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[a-z])(?=.*[!##$%^&*()_+])|(?=.*\d)(?=.*[A-Z])(?=.*[!##$%^&*()_+])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*()_+])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*()_+])).*$\R*

The pattern you can consider using is
^[^:\r\n]+:(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,32}$).*\R?
See the regex demo
Details:
^ - start of a line
[^:\r\n]+ - 1 or more chars other than a colon, CR and LF
: - a colon
(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,32}$) - negative lookahead that fails the match if there are seven to thirty-two chars other than line break chars immediately to the right of the current location, that contain both a digit, lower- or uppercase letter
.* - any 0 or more chars other than line break chars (the rest of the line)
\R? - an optional line break sequence
NOTE you may add more alternatives to the negative lookahead. E.g. replace (?=.*\d)(?=.*[a-zA-Z]) with (?:(?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[!##$%^&*()_+])) to also require digits and special chars of your choice in the pattern to negate.

Related

Validate Gmail username using regex

I'm trying validate Gmail username by using their self rule on create account page.
There are the rules:
First and last character of username must be an ascii letter (a-z) or number (0-9)
Username must be between 6 and 30 characters long
Only letters (a-z), numbers (0-9), and periods (.) are allowed
Username cannot contain consecutive periods (.)
Expecting results:
.carlos.so#gmail.com - invalid
carlos.so.#gmail.com - invalid
carlos..so#gmail.com - invalid
carlos_so#gmail.com - invalid
carlos.so#gmail.com - valid
Already tried the pattern above but without success:
(?!\.)[a-zA-Z_.]{6,30}(?!.*\.)
You could use:
^[a-zA-Z0-9](?=[a-zA-Z0-9.]{5,29}#)[a-zA-Z0-9]*(?:\.[a-zA-Z0-9]+)*#gmail\.com$
^ Start of string
[a-zA-Z0-9] Match a single char a-zA-Z0-9
(?=[a-zA-Z0-9.]{5,29}#) Assert 5-29 allowed chars to the right followed by #
[a-zA-Z0-9]* Match optional chars a-zA-Z0-9
(?:\.[a-zA-Z0-9]+)* Optionally repeat matching . and 1+ allowed chars
#gmail\.com Match #gmail.com
$ End of string
See a regex101 demo.
So a partial answer (I can't leave comments on stack overflow yet), would be
^[A-Za-z0-9][A-Za-z0-9.]{5,29}[A-Za-z0-9]#gmail.com
This will handle all cases except if two or more periods occur consecutively.
Is there a reason why you need it all in a single regex, or could you use two regexes? You weren't clear, but if that is a possibility it might be a good idea.

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 5-7 characters, or 6-8 if including a space (no special characters allowed)

I am trying to create a regex for some basic postcode validation. It doesn't need to provide full validation (in my usage it's fine to miss out the space, for example), but it does need to check for the number of characters being used, and also make sure there are no special characters other than spaces.
This is what I have so far:
^[\s.]*([^\s.][\s.]*){5,7}$
This mostly works, but it has two flaws:
It allows for ANY character, rather than just alphanumeric characters + spaces
It allows for multiple spaces to be inserted:
I have tried updating it as follows:
^[\s.]*([a-zA-Z0-9\s.][\s.]*){5,7}$
This seems to have fixed the character issue, but still allows multiple spaces to be inserted. For example, this should be allowed:
AB14 4BA
But this shouldn't:
AB1 4 4BA
How can I modify the code to limit the number of spaces to a maximum of one (it's fine to have none at all)?
With your current set of rules you could say:
^(?:[A-Za-z0-9]{5,7}|(?=.{6,8}$)[A-Za-z0-9]+\s[A-Za-z0-9]+)$
See an online demo
^ - Start-line anchor;
(?: - Open non-capture group for alternations;
[A-Za-z0-9]{5,7} - Just match 5-7 alphanumeric chars;
| - Or;
(?=.{6,8}$) - Positive lookahead to assert position is followed by at least 6-8 characters until the end-line anchor;
[A-Za-z0-9]+\s[A-Za-z0-9]+ - Match 1+ alphanumeric chars on either side of the whitespace character;
)$ - Close non-capture group and match the end-line anchor.
Alternatively, maybe a negative lookahead to prevent multiple spaces to occur (or at the start):
^(?!\S*\s\S*\s|\s)(?:\s?[A-Za-z0-9]){5,7}$
See an online demo where I replaced \s with [^\S\n] for demonstration purposes. Also, though being the shorter expression, the latter will take more steps to evaluate the input.

Extend regular expression

I want to find invoice numbers with a regex. The string has be longer than 3 char. It may contain signs like {., , /, _}, all numbers and it may contain one or two capital letters - those can stay alone or after each other. That is, what I'm currently trying, without success.
`([0-9-\.\\\/_]{,3})([A-Z]{0,2})?`
Here I have two examples, which should be matched:
019S836/03717008
DR094255
This should not be matched:
DRF094255
Can somebody help me please?
You can use
^(?!(?:[^A-Z]*[A-Z]){3})(?=\D*\d)[0-9A-Z.\\\/_-]{3,}$
See the regex demo.
Details:
^ - start of string
(?!(?:[^A-Z]*[A-Z]){3}) - a negative lookahead that fails the match if, immediately to the right of the current location (i.e. from the start of string), there are three occurrences of any zero or more chars other than uppercase ASCII letters followed with one uppercase ASCII letter
(?=\D*\d) - there must be at least one digit in the string
[0-9A-Z.\\\/_-]{4,} - four or more occurrences of digits, uppercase letters, ., \, /, _ or -
$ - end of string.

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.