字符串精确匹配的正则表达式 [英] Regular expression for exact match of a string

查看:69
本文介绍了字符串精确匹配的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用正则表达式匹配两个密码.例如,我有两个输入123456"和1234567",那么结果应该不匹配(假).当我输入123456"和123456"时,结果应该是匹配的(真).

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?

推荐答案

如果你在一个变量中有一个输入密码并且你想要精确匹配 123456 那么锚点会帮助你:

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

/^123456$/

在 perl 中,匹配密码的测试类似于

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

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

bart kiers 是对的,为什么不使用 strcmp() 呢?每种语言都有它自己的方式

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

这篇关于字符串精确匹配的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆