237

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Chirayu
  • 4,693
  • 7
  • 28
  • 46

7 Answers7

258

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 :)

Community
  • 1
  • 1
user237419
  • 8,829
  • 4
  • 31
  • 38
  • 3
    **Important**: In Perl and PCRE, you want `\z` instead of `$`. `/^123456$/` matches both `123456` and `123456` followed by a line feed, while `/^123456\z/` matches only `123456`. – ikegami Sep 29 '19 at 09:58
  • Thanks for this. I found that Voidtools Everything search did not need need the / using the regex option. Just the `^` and the `$` worked, expecting those as special characters already. Useful for runtime type tools that know/expect it already. – Andrew Marais May 03 '23 at 11:00
201

In malfaux's answer '^' and '$' has been used to detect the beginning and the end of the text.
These are usually used to detect the beginning and the end of a line.
However this may be the correct way in this case.
But if you wish to match an exact word the more elegant way is to use '\b'. In this case following pattern will match the exact phrase'123456'.

/\b123456\b/

prageeth
  • 7,159
  • 7
  • 44
  • 72
  • Are you sure that it works in that way? ;) Please use a regex tester like [this](http://www.regular-expressions.info/javascriptexample.html). Use "show match" button and you will see why it matches. BTW remember '.' is a special char and good to escape it. (and good to think twice before down voting :D ). – prageeth Sep 29 '13 at 03:16
  • You overrode my edit and may lead people to introduce bugs into their programs if they are trying to make exact matches against strings like URIs or passwords (the latter was the subject of the OP) that often contain non-word characters. I've seen this bug in production code--it is not good. If you will not allow me to clarify your answer, please do so yourself. – 0x1mason Jan 01 '14 at 16:30
  • 3
    If you think my answer is misleading or wrong, you still can post your own answer(as other people do) or place a comment under my post explaining your suggestion. – prageeth Jan 02 '14 at 05:03
  • 39
    Your answer is misleading and will cause many people serious problems. E.g., using your approach \bhttp://www.foo.com\b will match http://http://www.bar.com/?http://www.foo.com, which is most definitely not an exact match, as requested in the OP. This will cause serious problems for people working with URLs or passwords (which, again, is what the OP requested) that contain non-word characters. You should clarify your answer so others are not led astray. – 0x1mason Jan 02 '14 at 17:53
  • @0x1mason Read my very first comment again. Still it is 100% valid. – prageeth Jan 07 '14 at 06:24
  • 2
    You'll have to explain. '.' is a red herring. \bhttp://www\.foo\.com\b still produces a match for http://http://www.bar.com/?http://www.foo.com. That's not what the OP wants. – 0x1mason Jan 07 '14 at 14:45
  • A simpler false match example is \bfoo\b matches www.foo.com. And \bfoo\b matches bar-foo-bar. It goes on and on. All of them produce matches when the OP needs them to /not/ match. – 0x1mason Jan 07 '14 at 14:59
  • 1
    @0x1mason is correct that `\bhttp:\/\/www.foo.com\b` will match `http://www.foo.com=?http://www.foo.com` along with the URL as is. The first response, using the `^` (beginning) and `$` (end) is the best method via these testers: https://regex101.com and http://regexr.com (the tester included in a link below is not very user friendly, these are much better) – twknab Feb 11 '17 at 11:26
  • ```^123456$``` did not work for the natural language data (= 'normal text') I have but ```\b123\b``` worked. Used Python. Word frequency bar chart is nice and tidy now. – Simone Jul 06 '23 at 09:28
55
(?<![\w\d])abc(?![\w\d])

this makes sure that your match is not preceded by some character, number, or underscore and is not followed immediately by character or number, or underscore

so it will match "abc" in "abc", "abc.", "abc ", but not "4abc", nor "abcde"

Aedna
  • 761
  • 6
  • 5
  • 2
    The title of the question is misleading; he's trying to make sure two *whole strings* are exactly the same. Also, `\w` matches digits as well as letters, so `[\w\d]` is redundant. – Alan Moore Apr 17 '14 at 02:54
  • 1
    nice. see https://www.regextester.com/?fam=113232 for "fiddle" i did to try it out – unsynchronized Jan 02 '20 at 04:54
  • how to design it to match "(abc)" with parenthesis – Usama Ahmed Feb 27 '21 at 12:28
  • 1
    Warning: In case you are planning to use the lookbehind operator(?<!) in react or other frameworks, It won't work in iOS devices. No error shown in console either. You can read more here [https://stackoverflow.com/questions/51568821/works-in-chrome-but-breaks-in-safari-invalid-regular-expression-invalid-group](https://stackoverflow.com/questions/51568821/works-in-chrome-but-breaks-in-safari-invalid-regular-expression-invalid-group) – Santhosh Jan 29 '22 at 16:23
6

A more straight forward way is to check for equality

if string1 == string2
  puts "match"
else
  puts "not match"
end

however, if you really want to stick to regular expression,

string1 =~ /^123456$/
kurumi
  • 25,121
  • 5
  • 44
  • 52
  • @Kurumi I'm trying to make java code that search for specific word in txt file and i want to use regx so could i use that pattern /^myword$/ with String.match(/^myword$/ ) to search for the word in that txt? – Antwan Mar 30 '14 at 00:45
5

You may also try appending a space at the start and end of keyword: /\s+123456\s+/i.

Benoit Garret
  • 14,027
  • 4
  • 59
  • 64
Bhushan Lodha
  • 6,824
  • 7
  • 62
  • 100
1

If you are working in Python, re has a re.fullmatch method, bit easier then modifying the string

xerxes
  • 11
  • 1
0

you can match an exact string with JavaScript by using the JavaScript string’s match method with a regex pattern

let str1 = '123456' ;
console.log(str1.match(/^123456$/)) ; //['123456', index: 0, input: '123456', groups: undefined]
console.log(str1.match(/^1234567$/)) ; //null
Avnish Jayaswal
  • 161
  • 1
  • 4