[英]Regular Expression to Match String Exactly?
I'll preface this question by mentioning that while I'm far from a regular expressions guru, they are not completely foreign to me.我将在这个问题的开头提到,虽然我离正则表达式大师还很远,但它们对我来说并不完全陌生。 Building a regular expression to search for a pattern inside a particular string generally isn't a problem for me, but I have a (maybe?) unique situation.
构建正则表达式来搜索特定字符串中的模式通常对我来说不是问题,但我有一个(也许?)独特的情况。
I have a set of values, say:我有一组值,比如:
028938
028938
DEF567987DEF567987
390987.456390987.456
GHI345928.039GHI345928.039
I want to match a certain set of strings, such as:我想匹配一组特定的字符串,例如:
In the above examples, the first and third values should be matched.在上面的例子中,第一个和第三个值应该匹配。
I'm using the regular expressions:我正在使用正则表达式:
[0-9]{6}
[0-9]{6}.[0-9]{3}
Unfortunately, since all the above examples contain the specified pattern, all values are matched.不幸的是,由于上述所有示例都包含指定的模式,因此所有值都匹配。 This is not my intention.
这不是我的本意。
So my question, in a nutshell, is how to write a regular expression that matches a string exactly and completely, with no additional characters to the right or left of the matched pattern?所以我的问题,简而言之,是如何编写一个与字符串完全匹配的正则表达式,在匹配模式的右侧或左侧没有额外的字符? Is there a term for this type of matching?
这种类型的匹配有术语吗? (Google was no help.) TIA
(谷歌没有帮助。)TIA
use ^
and $
to match the start and end of your string使用
^
和$
匹配字符串的开头和结尾
^[0-9]{6}$
^[0-9]{6}\.[0-9]{3}$
Reference: http://www.regular-expressions.info/anchors.html参考: http : //www.regular-expressions.info/anchors.html
Also, as noted by Mikael Svenson, you can use the word boundary \\b
if you are searching for this pattern in a larger chunk of text.此外,正如 Mikael Svenson 所指出的,如果您要在较大的文本块中搜索此模式,则可以使用单词边界
\\b
。
Reference: http://www.regular-expressions.info/wordboundaries.html参考: http : //www.regular-expressions.info/wordboundaries.html
You could also write both those regexes in one shot你也可以一次写出这两个正则表达式
^\d{6}(\.\d{3})?$
You can use ^
to require the matching at the start of a line and $
to require the end of a line您可以使用
^
要求在行首匹配,使用$
要求匹配行尾
^[0-9]{6}\.[0-9]{3}$
[0-9] can also be written as \\d [0-9] 也可以写成 \\d
^\d{6}\.\d{3}$
You can also use \\b
for word boundaries if you want to match your pattern in a line with eg.如果您想在一行中匹配您的模式,您也可以使用
\\b
作为单词边界。 spaces in them其中的空间
\btest\b
will match the word test
in this line将匹配这一行中的单词
test
this is a test for matching
^\d{6}$
^\d{6}\.\d{3}$
are the correct patterns you can test them 6 digits only and 6 digits dot 3 digits .是正确的模式,您可以仅测试6 位数字和6 位数字点 3 位数字。
^\d{6}((\.\d{3}$)|$)
will match either 6 digits or 6 digits dot 3 digits将匹配6 位数字或 6 位数字点 3 位数字
匹配这个正则表达式:
"^\d{6}((\.\d{3}$)|$)"
i think you want something like this:我想你想要这样的东西:
"^\d{6}(\.\d{3})?$"
you need to escape the "dot" as it is "any" character in regexp.您需要转义“点”,因为它是正则表达式中的“任何”字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.