Regular expression for exact match of a string
Asked Answered
C

7

247

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?

Curnin answered 22/4, 2011 at 6:24 Comment(7)
Why do you want regex in this case? Checking for (exact) equality should really be done without regex.Calton
Because we look up how to replace text in a Bash script and get an answer that involves Perl and regex.Satin
having the same issue a better solution ^(123456) will give the results that you wantEctophyte
Possible duplicate of Match whole stringUnanimity
@canbax Why would this be a funny question exactly?Calciferous
why would somebody need to use regex for exact string matching? Regex is simply designed and used for pattern matching. Bart Kiers already stated my idea.Dayna
why not just use ===?Zelaya
D
268

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

Daphinedaphna answered 22/4, 2011 at 6:29 Comment(2)
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.Englut
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.Chubby
L
207

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/

Larios answered 25/10, 2012 at 9:41 Comment(9)
Are you sure that it works in that way? ;) Please use a regex tester like this. 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 ).Larios
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.Ramiah
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.Larios
Your answer is misleading and will cause many people serious problems. E.g., using your approach \bhttp://www.foo.com\b will match 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.Ramiah
@Ramiah Read my very first comment again. Still it is 100% valid.Larios
You'll have to explain. '.' is a red herring. \bhttp://www\.foo\.com\b still produces a match for http://www.bar.com/?http://www.foo.com. That's not what the OP wants.Ramiah
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.Ramiah
@Ramiah 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: regex101.com and regexr.com (the tester included in a link below is not very user friendly, these are much better)Alopecia
^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.Sting
O
54
(?<![\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"

Orv answered 16/4, 2014 at 19:1 Comment(4)
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.Audsley
nice. see regextester.com/?fam=113232 for "fiddle" i did to try it outVilleinage
how to design it to match "(abc)" with parenthesisDaguerre
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://mcmap.net/q/118885/-works-in-chrome-but-breaks-in-safari-invalid-regular-expression-invalid-group-specifier-name-ltRendition
M
5

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$/
Mohammadmohammed answered 22/4, 2011 at 6:33 Comment(1)
@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?Policlinic
P
4

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

Pesthouse answered 7/11, 2011 at 11:36 Comment(0)
D
0

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

Delgadillo answered 8/4, 2023 at 7:6 Comment(0)
S
-1

If I understand the question right, you are looking to match only one EXACT string and not if a sub-string?

/^123456$/

Simply anchor the beginning and the end of the string. You can choose more than one string with the | (or)..

/^123456$|^12345$/

or

/^bedroom$|^closet$/

Without the anchors, regex will match the first or any sub-string that matches in a string depending on your flags.

I would only use regex to match allowed characters. To verify a match, you can use logic.

$some_regex = '/^[a-zA-Z0-9\S]{8,20}$/'; // whatever (matches all chars)
$password = '123456';
$password2 = '123456w@';

if(preg_match($some_regex, $password) && preg_match($some_regex, $password2) && $password == $password2){
    return true;
}
else{
    return false;
}
Spearing answered 28/3 at 4:43 Comment(1)
Please don’t duplicate answers. There is already an answer from 2011 with this code.Tegucigalpa

© 2022 - 2024 — McMap. All rights reserved.