Java 正则表达式精确匹配字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3479324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression to Match String Exactly?
提问by Craig Otis
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
DEF567987
390987.456
GHI345928.039
028938
DEF567987
390987.456
GHI345928.039
I want to match a certain set of strings, such as:
我想匹配一组特定的字符串,例如:
- Strings composed of exactly 6 digits
- Strings composed of exactly 6 digits, a decimal, followed by exactly 3 more digits
- 由 6 位数字组成的字符串
- 由 6 位数字组成的字符串,一个小数,后跟 3 位数字
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 containthe 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
采纳答案by CaffGeek
use ^
and $
to match the start and end of your string
使用^
and$
匹配字符串的开头和结尾
^[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})?$
回答by Mikael Svenson
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. spaces in them
\b
如果您想在一行中匹配您的模式,您也可以用于单词边界,例如。其中的空间
\btest\b
will match the word test
in this line
将匹配test
此行中的单词
this is a test for matching
回答by Mikael Svenson
^\d{6}$
^\d{6}\.\d{3}$
are the correct patterns you can test them 6 digits onlyand 6 digits dot 3 digits.
是正确的模式,您可以仅测试6 位数字和6 位数字点 3 位数字。
^\d{6}((\.\d{3}$)|$)
will match either 6 digits or 6 digits dot 3 digits
Rubularis your friend!
Rubular是你的朋友!
回答by Gopi
Match this regex:
匹配这个正则表达式:
"^\d{6}((\.\d{3}$)|$)"
回答by Ryan Conrad
i think you want something like this:
我想你想要这样的东西:
"^\d{6}(\.\d{3})?$"
you need to escape the "dot" as it is "any" character in regexp.
您需要转义“点”,因为它是正则表达式中的“任何”字符。