Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow any character in RegEx?

Tags:

regex

Current I use the following RegEx for the user to enter a password

^\w{8,16}$

Now as I understand, \w only allows a-z, A-Z, 0-9 and _ character. What I want to do is to allow ANY character but the length to be between 8 and 16. How do I get about doing it? Thanx a lot in advance :)

like image 954
Ranhiru Jude Cooray Avatar asked Feb 10 '11 04:02

Ranhiru Jude Cooray


People also ask

How do I allow special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do I match a specific character in regex?

There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .


2 Answers

Firstly, use a word count for what you need rather than regex.

If you really must, then .{8,16} should work, the . matches a single char, no matter what it is.

EDIT: To preempt your next question which will surely be, what is a good password validation regular expression, you might want to check out some of these blogs:

http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/

http://www.zorched.net/2009/05/08/password-strength-validation-with-regular-expressions/

OR just look up 'password validation stackoverflow' on google

like image 174
Joe Avatar answered Nov 15 '22 07:11

Joe


Try this:

^.{8,16}$

The dot matches a single character, without caring what that character is. The only exception are newline characters. By default, the dot will not match a newline character.

For the details, please visit: http://www.regular-expressions.info/dot.html.

like image 45
Arief Avatar answered Nov 15 '22 06:11

Arief