Java REGEX匹配字符串中的确切位数 [英] Java REGEX to match an exact number of digits in a string
问题描述
我试图在问题历史中找到我的问题的答案,但他们只是回来了一千多个,在扫描了几十个匹配的答案之后我放弃了。
所以这是我的问题。
I tried to find the answer to my problem in the questions history but they just come back in more than one thousand and after scanning through a few tens of matching answers I gave up. So here is my problem.
我希望能够在字符串中找到正好六位数的第一个序列。
给定字符串
一些文本987654321和一些其他文本123456和一些其他文本再次654321和更多文本到底
我想找到与123456序列匹配的正则表达式。
I want to be able to find the first sequence of exactly six digits in a string. Given the string "Some text 987654321 and some more text 123456 and some other text again 654321 and more text in the end" I want to find the regex that will match the 123456 sequence.
我是regex的新手,关于它如何运作的简短解释会有很大的帮助。
I am new to regex and a short explanation about how it works will help a lot.
谢谢提前
推荐答案
您可以使用模式(?<!\ d)\ d {6}(?!\ d)
,表示一个字符串位置,前面没有数字;后跟正好六位数字;后跟一个字符串位置,后面没有一个数字。 (符号(?<!...)
,称为负后观断言,表示前面没有 ...
。符号(?!...)
,称为否定先行断言,表示未跟随 ...
。符号 \d
表示数字。符号 {n}
表示 n 次,例如 \d {6}
表示六位。)
You can use the pattern (?<!\d)\d{6}(?!\d)
, which means "a string-position that is not preceded by a digit; followed by exactly six digits; followed by a string-position that is not followed by a digit". (The notation (?<!...)
, known as a negative lookbehind assertion, means "not preceded by ...
". The notation (?!...)
, known as a negative lookahead assertion, means "not followed by ...
". The notation \d
means a digit. The notation {n}
means "n times", so that e.g. \d{6}
means "six digits".)
这可能如下所示:
final String number;
{
final Matcher m = Pattern.compile("(?<!\\d)\\d{6}(?!\\d)").matcher(input);
if(m.find())
number = m.group(); // retrieve the matched substring
else
number = null; // no match found
}
注意:以前的这个答案的版本建议使用单词边界, \ b
;但是你的一条评论表明,繁体中文字符可能会紧跟在前面或后面,这些字符被认为是单词字符(因此不会触发单词边界),所以我改变了它。
Note: a previous version of this answer suggested the use of word boundaries, \b
; but one of your comments suggests that the digits might be immediately preceded or followed by Traditional Chinese characters, which are considered word characters (and therefore wouldn't trigger a word boundary), so I've changed that.
这篇关于Java REGEX匹配字符串中的确切位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!