Regex pattern for a password - regex

I need a regex pattern that validates a password format.
The rules are:
Minimum 8 chars in total
at least two letters
at least two digits or symbols
I came up with the following:
/((?=.*[0-9\#\&#\$\?\%!\|(){}[]])(?=.*[a-zA-Z]).{8,})/
It will look if both occure once, but I need it toch validate if they occur at least twice.
If I add the {2,} like this:
/((?=.*[0-9\#\&#\$\?\%!\|(){}[]]{2,})(?=.*[a-zA-Z]{2,}).{8,})/
Then the following doesnt work for example: a1a1a1a1a1
Can anybody help me?

This is how you do it, using positive lookaheads: http://regex101.com/r/uW0yI4
/^(?=.*[a-z].*[a-z])(?=.*[!"#...\d].*[!"#...\d]).{8,}$/gmi
Just replace !"#... with all the symbols you want to match.
Note: the multiline flag might be unnecessary for your applications.

This should give you what you're after:
^((?=(.*[\d0-9\#\&#\$\?\%!\|(){}[\]]){2,})(?=(.*[a-zA-Z]){2,}).{8,})$

Related

Regex for password - Not maximum 4 repeating characters [duplicate]

My experience with regular expressions is limited and I've been reading various tutorials and posts on negation and negative lookahead, etc, but nothing seems to quite match my situation. I'm trying to create an attribute in ASP.NET MVC3 for password complexity. Part of the validation includes a minimum number of repeated characters. For the current project the limit is 3, but I want to generalize it.
Initially, I was using #"(.)\1{3,}" to test for 4 or more repeated characters and then negating that result. I can't do that now because I need to create a ModelClientValidationRegexRule object, which will only work with positive results. As such, the negation must be done inside the regex itself. Every way I've tried to use negative lookahead fails, e.g. #".*(?!(.)\1{3,})". Any ideas?
Turn the problem around: a character can be followed by at most 3 of the same. Then it must be followed by something else. Finally, the whole string must consist of sequences like this. In the perl flavor:
^((.)\2{0,3}(?!\2))*$
You need to put the .* inside the lookahead:
(?!.*?(.)\1{3,})
The way you're doing it, the .* consumes the whole string, then the lookahead asserts that there aren't four of the same character after the end of the string, which of course is always true.
I used a non-greedy star in my lookahead because it seemed more appropriate, but greedy will work too--it just has to be inside the lookahead.
I'm assuming this is just one of several lookaheads, that being the usual technique for validating password strength in a regex. And by the way, while regex-negation is appropriate, you would have gotten more responses to your question much more quickly if you had used the regex tag as well.
I used the simple ^(.)(?!\1\1){8,}$ for a 8 or more character that doesn't have any characters that repeat more than twice.
I think, use this regex .*(.).*\1+.* to matches existd repeated characters. But for four, depend on you.
Good luck!
Find char in the group then match repeats
(.).*(\1{3,})

Regex Expression Query

I have data as
CVE-2011-0573,
CVE-2011-0606,
(CVE-2011-0565)
CVE-2011-0598,
CVE-2011-0593.
((CVE-2011-0593.)
Could you please help me writing RegEx so I only get ABC-####-#### ? The last four digit may vary, so it can have e.g. three or five digits, but most likely not more than ten. Also the expresion may contain some spaces in the end, so those need to be removed as well.
You can use this regex for matching:
[A-Z]{3}-[0-9]{4}-[0-9]{3,10}
If you have multiline flag available then use
/^[A-Z]{3}-[0-9]{4}-[0-9]{3,10}$/mg

Using Regex to validate account numbers

Could you please help me with a regular expression that would allow strings in several valid formats?
The formats are:
5digits dash 6digits
5digits dash 7digits
6digits dash 6digits
7digits dash 7digits
7digits dash 8 digits
I am asking this because I know almost nothing about regex.
Thanks.
I would suggest you to keep it simple to this regex:
/^\d{5,7}-\d{6,8}$/
Even though your specified formats can be validated by following regex:
^\d{5}-\d{6,7}|\d{7}-\d{7,8}|\d{6}-\d{6}$
but that won't be very maintainable for future changes.

What is wrong with my simple regex that accepts empty strings and apartment numbers?

So I wanted to limit a textbox which contains an apartment number which is optional.
Here is the regex in question:
([0-9]{1,4}[A-Z]?)|([A-Z])|(^$)
Simple enough eh?
I'm using these tools to test my regex:
Regex Analyzer
Regex Validator
Here are the expected results:
Valid
"1234A"
"Z"
"(Empty string)"
Invalid
"A1234"
"fhfdsahds527523832dvhsfdg"
Obviously if I'm here, the invalid ones are accepted by the regex. The goal of this regex is accept either 1 to 4 numbers with an optional letter, or a single letter or an empty string.
I just can't seem to figure out what's not working, I mean it is a simple enough regex we have here. I'm probably missing something as I'm not very good with regexes, but this syntax seems ok to my eyes. Hopefully someone here can point to my error.
Thanks for all help, it is greatly appreciated.
You need to use the ^ and $ anchors for your first two options as well. Also you can include the second option into the first one (which immediately matches the third variant as well):
^[0-9]{0,4}[A-Z]?$
Without the anchors your regular expression matches because it will just pick a single letter from anywhere within your string.
Depending on the language, you can also use a negative look ahead.
^[0-9]{0,4}[A-Za-z](?!.*[0-9])
Breakdown:
^[0-9]{0,4} = This look for any number 0 through 4 times at the beginning of the string
[A-Za-z] = This look for any characters (Both cases)
(?!.*[0-9]) = This will only allow the letters if there are no numbers anywhere after the letter.
I haven't quite figured out how to validate against a null character, but that might be easier done using tools from whatever language you are using. Something along this logic:
if String Doesn't equal $null Then check the Rexex
Something along those lines, just adjusted for however you would do it in your language.
I used RegEx Skinner to validate the answers.
Edit: Fixed error from comments

Regex href match a number

Well, here I am back at regex and my poor understanding of it. Spent more time learning it and this is what I came up with:
/(.*)
I basically want the number in this string:
510973
My regex is almost good? my original was:
"/<a href=\"travis.php?theTaco(.*)\">(.*)<\/a>/";
But sometimes it returned me huge strings. So, I just want to get numbers only.
I searched through other posts but there is such a large amount of unrelated material, please give an example, resource, or a link directing to a very related question.
Thank you.
Try using a HTML parser provided by the language you are using.
Reason why your first regex fails:
[0-9999999] is not what you think. It is same as [0-9] which matches one digit. To match a number you need [0-9]+. Also .* is greedy and will try to match as much as it can. You can use .*? to make it non-greedy. Since you are trying to match a number again, use [0-9]+ again instead of .*. Also if the two number you are capturing will be the same, you can just match the first and use a back reference \1 for 2nd one.
And there are a few regex meta-characters which you need to escape like ., ?.
Try:
<a href=\"travis\.php\?theTaco=([0-9]+)\">\1<\/a>
To capture a number, you don't use a range like [0-99999], you capture by digit. Something like [0-9]+ is more like what you want for that section. Also, escaping is important like codaddict said.
Others have already mentioned some issues regarding your regex, so I won't bother repeating them.
There are also issues regarding how you specified what it is you want. You can simply match via
/theTaco=(\d+)/
and take the first capturing group. You have not given us enough information to know whether this suits your needs.