Yet another password validating regular expression - regex

I've gone through multiple examples to validate passwords via regular expression, but none of them quite fit what I am looking for. I've been using trial and error to build my own, but without complete success.
Here is the regular expression that so far is the closest match for what I am looking for:
(?=.*?[a-z]{3,})(?=.*?[A-Z]{3,})(?=.*?[0-9]{2,})[a-zA-Z0-9]{8,24}
The password should have three lowercase and three uppercase alphabets and two numbers. Password length should be between 8 and 24 characters. Special characters are not looked for, they can be used as long as other requirements are met.
The regular expression above matches ABCdef12 but does not match Ad1Be1Cf. How I should modify the regular expression so it also matches the latter example?

Use look aheads for the content assertion, and a simple regex for the length:
^(?=(.*[a-z]){3})(?=(.*[A-Z]){3})(?=(.*\d){2}).{8,24}$
See demo
I'm reasonably confident this this the shortest regex that will work for you.

(?=.{8,24}$)(?=.*?[a-z].*?[a-z].*?[a-z])(?=.*?[A-Z].*?[A-Z].*?[A-Z])(?=.*?\d.*?\d)(^.*$)
You can use this.It uses lookahead to test all conditions.
See Demo.
http://regex101.com/r/yX3eB5/9

Related

Reg expression to get a string starting from particular string

I'm trying to write a regular expression which returns a string after a particular string.
For example:
The string is
"<https://meraki/api/v1/sm/devices?fields%5B%5D=imei%2Ciccid%2ClastConnected%2CownerEmail%2C+ownerUsername%2CphoneNumber&perPage=1000&startingAfter=0>; rel=first"
result I'm expecting is -- first.
Here is the expression i'm using
(?<=rel=\s").*(?=\)
Okay so this should work:
(?<=rel[=])[^"]*
I would advise looking over the syntax of regex again, because yours was not even matching the colons correctly. Look behinds (?<=pattern) match before the pattern you want to capture. Likewise look aheads (?=pattern) match after the pattern.
You can test your regex online here (or many other sites). They will show you the matching groups and errors, but will also explain what certain parts of the pattern do.

Validation, regular expression to allow some characters

I'm trying to validate a telephone number which should contain only digits with some special characters like () , - . I've tried the following /^[0-9,()-]+$/ but it is showing an error. The special characters are optional, other than those special characters and digits are not allowed.
I'd suggest you take a look at Perl's RegEx tutorial and www.regular-expressions.info for a better understanding of RegEx's.
The pattern you have provided (even if syntactically corrected) is not appropriate for your case (phone numbers), as it matches strings like 78,(-34.
The first step to write a correct RegEx is understanding the pattern you need. For your case, you have to think about the different strings that will match as a phone number. Some examples:
Match:
(500)-8764531
500-8764531
(+9821)-76787600
76787600,01
No match
(500)-876453187645318764531
500,8764531
(+9821-76787600
76787600,01)
The next step is to build a RegEx that matches those patterns. After that you have to test your pattern with different boundary cases and if required do the previous steps again.

Creating a regular expression to match words of varying lengths

I'm writing a regular expression to parse a logfile and I'm having trouble figuring out how to establish a range(?) of sorts for a particular expression. In this case specifically, my logfile contains various severities:
(['EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG'])
I'm basically wondering how I'd write regular expression to match all of those. I understand most digit work, but characters are posing difficult issues for me.
this regex will match all these entries: [A-Za-z]{1,}
basically it says match all patterns that have only chars from A to Z or a to z with the lenght of at least one char.
for more information see this: regex cheat-sheet
and try your regex here: http://gskinner.com/RegExr/

Regex for just only numbers

I haven't used regular expressions soo much, so I'm having difficulty . I want regex that only validates that the field contains digits, but that does not care about how many.
It should approve 77 and 2377? But do not approve 77.43 or xyz777.
How can I get this using regular expression? Is this expression ^[0-9]+$ ok or not
It's OK. You can just use ^\d+$ for all it matters anyway.
Yes, this regex is perfectly valid and does what you think it does, although if your regex engine supports this you could use \d, whichs stands for [0-9].
A simpler regex would be to invert your match and check for non-digit numbers: \D.

is it the right reqular expression

i have following regular expression but it's not working properly it takes only three values after # sign but i want it to be any number length
"/^[a-zA-Z0-9_\.\-]+\#([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/"
this#thi This is validated
this#this It is not validating this expression
Can you please tell me what's the problem with the expression...
Thanks
If you want your regex to match "any number length" then why are you using {2,4}?
I think a better example of the strings you're trying to match might give others a better idea of what you want, because based on your regex it is a bit confusing what you're looking for.
Try this:
^[a-zA-Z0-9_.-]+#([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$
The main problem is that you didn't escape the dot: \.. In regular expression the dot matches everything (mostly), making your regex quite liberal.