Regular expression for exact match of a string

Chirayu picture Chirayu · Apr 22, 2011 · Viewed 605k times · Source

I want to match two passwords with regular expression. For example I have two inputs "123456" and "1234567" then the result should be not match (false). And when I have entered "123456" and "123456" then the result should be match (true).

I couldn't make the expression. How do I do it?

Answer

user237419 picture user237419 · Apr 22, 2011

if you have a the input password in a variable and you want to match exactly 123456 then anchors will help you:

/^123456$/

in perl the test for matching the password would be something like

print "MATCH_OK" if ($input_pass=~/^123456$/);

EDIT:

bart kiers is right tho, why don't you use a strcmp() for this? every language has it in its own way

as a second thought, you may want to consider a safer authentication mechanism :)