简体   繁体   English

使用Regex比较字符串以识别完全相同的字符串

[英]Compare string using Regex for recognizing exactly the same strings

I have a pattern like: 我有一个像这样的模式:

\[[0-9],([0-9]|1[0-4]),[0-9],([0-9]|1[0-4])]

that perfectly matches the strings that are in form of 与字符串形式的字符串完全匹配

eg 例如

[1,1,1,2]
[5,6,5,7]

I want the string like 我想要像这样的字符串

[1,1,1,2]kwfkbkkbdobug

be recognized as a false or invalid and not compatible with the pattern, while by using current pattern the string that I mentioned will be recognized as a valid string because it has a [1,1,1,2] string inside regardless of existence of kwfkbkkbdobug . 会被识别为错误或无效且与模式不兼容,而通过使用当前模式,我提到的字符串将被识别为有效字符串,因为无论内部是否存在[1,1,1,2]字符串kwfkbkkbdobug

What would be the best pattern in order to satisfy the need? 满足需求的最佳模式是什么?

Just make use of the beginning of line anchor and the end of line anchor: 只需利用行锚的开头和行锚的结尾即可:

^\[[0-9],([0-9]|1[0-4]),[0-9],([0-9]|1[0-4])]$

^ is the beginning of line anchor which matches only at the beginning of a line. ^是行锚的开始,仅在行的开头匹配。

$ is the end of line anchor which matches only at the end of a line. $是行锚的结尾,仅在行末匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM