Java正则表达式匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4463867/
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
Java regular expression match
提问by user152508
I need to match when a string begins with number, then a dot follows, then one space and 1 or more upper case characters. The match must occur at the beginning of the string. I have the following string.
当字符串以数字开头时,我需要匹配,然后是一个点,然后是一个空格和 1 个或多个大写字符。匹配必须出现在字符串的开头。我有以下字符串。
1. PTYU fmmflksfkslfsm
The regular expression that I tried with is:
我尝试使用的正则表达式是:
^\d+[.]\s{1}[A-Z]+
And it does not match. What would a working regular expression be for this problem?
它不匹配。这个问题的工作正则表达式是什么?
采纳答案by T.J. Crowder
(Sorry for my earlier error. Brain now firmly engaged. Er, probably.)
(对不起,我之前的错误。大脑现在坚定地参与了。呃,可能是。)
This works:
这有效:
String rex = "^\d+\.\s\p{Lu}+.*";
System.out.println("1. PTYU fmmflksfkslfsm".matches(rex));
// true
System.out.println(". PTYU fmmflksfkslfsm".matches(rex));
// false, missing leading digit
System.out.println("1.PTYU fmmflksfkslfsm".matches(rex));
// false, missing space after .
System.out.println("1. xPTYU fmmflksfkslfsm".matches(rex));
// false, lower case letter before the upper case letters
Breaking it down:
分解它:
^
= Start of string\d+
= One or more digits (the\
is escaped because it's in a string, hence\\
)\.
= A literal.
(or your original[.]
is fine) (again, escaped in the string)\s
= One whitespace char (no need for the{1}
after it) (I'll stop mentioning the escapes now)\p{Lu}+
= One or more upper case letters (using the proper Unicode escape — thank you, tchrist, for pointing this out in your comment below. In English terms, the equivalent would be[A-Z]+
).*
= Anything else
^
= 字符串的开始\d+
= 一个或多个数字(\
被转义,因为它在一个字符串中,因此\\
)\.
= 一个文字.
(或者你的原版没问题[.]
)(同样,在字符串中转义)\s
= 一个空白字符({1}
后面不需要)(我现在不再提及转义)\p{Lu}+
= 一个或多个大写字母(使用正确的 Unicode 转义符——谢谢你,tchrist,在你下面的评论中指出这一点。用英语来说,相当于[A-Z]+
).*
= 别的
See the documentation herefor details.
有关详细信息,请参阅此处的文档。
You only need the .*
at the end if you're using a method like String#match
(above) that will try to match the entirestring.
.*
如果您使用像String#match
(上面)这样的方法尝试匹配整个字符串,则只需要最后。
回答by khachik
"^[0-9]+\. [A-Z]+ .+"
"^[0-9]+\. [A-Z]+ .+"
回答by AlexR
It depends which method are you using. I think it will work if you use Matcher.find(). It will not work if you are using Matcher.matches() because match works on whole line. If you are using matches() fix your pattern as following:
这取决于您使用哪种方法。如果您使用 Matcher.find(),我认为它会起作用。如果您使用 Matcher.matches() 它将不起作用,因为 match 在整行上都有效。如果您使用matches() 修复您的模式如下:
^\d+\.\s{1}[A-Z]+.*
(pay attention on trailing .*
)
(注意尾随.*
)
And I'd also use \.
instead of [.]
. It is more readable.
而且我也会使用\.
代替[.]
. 它更具可读性。