Regular expression to enforce complex passwords, matching 3 out of 4 rules

Don’t use one regex to check it then. if (password.length < 8) alert(“bad password”); var hasUpperCase = /[A-Z]/.test(password); var hasLowerCase = /[a-z]/.test(password); var hasNumbers = /\d/.test(password); var hasNonalphas = /\W/.test(password); if (hasUpperCase + hasLowerCase + hasNumbers + hasNonalphas < 3) alert(“bad password”); If you must use a single regex: ^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}$ This regex is not optimized … Read more

Regular expression pipe confusion

The first pattern without the parenthesis is equivalent to /(^a)|(b$)/. The reason is, that the pipe operator (“alternation operator”) has the lowest precedence of all regex operators: http://www.regular-expressions.info/alternation.html (Third paragraph below the first heading)

What regular expression will match valid international phone numbers?

\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d| 2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]| 4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$ Is the correct format for matching a generic international phone number. I replaced the US land line centric international access code 011 with the standard international access code identifier of ‘+’, making it mandatory. I also changed the minimum for the national number to at least one digit. Note that if you … Read more

Searching for UUIDs in text with regex

The regex for uuid is: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} If you want to enforce the full string to match this regex, you will sometimes (your matcher API may have a method) need to surround above expression with ^…$, that is ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$

Regex – Should hyphens be escaped? [duplicate]

Correct on all fronts. Outside of a character class (that’s what the “square brackets” are called) the hyphen has no special meaning, and within a character class, you can place a hyphen as the first or last character in the range (e.g. [-a-z] or [0-9-]), OR escape it (e.g. [a-z\-0-9]) in order to add “hyphen” … Read more

How can I match nested brackets using regex?

Many regex implementations will not allow you to match an arbitrary amount of nesting. However, Perl, PHP and .NET support recursive patterns. A demo in Perl: #!/usr/bin/perl -w my $text=”(outer (center (inner) (inner) center) ouer) (outer (inner) ouer) (outer ouer)”; while($text =~ /(\(([^()]|(?R))*\))/g) { print(“———-\n$1\n”); } which will print: ———- (outer (center (inner) (inner) center) … Read more