Java - 正则表达式匹配多个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23961684/
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 - Regex Match Multiple Words
提问by Subhash Ramesh
Lets say that you want to match a string with the following regex: ".when is (\w+)." - I am trying to get the event after 'when is'
假设您想将字符串与以下正则表达式匹配:“. when is (\w+).” - 我试图在 'when is' 之后获取事件
I can get the event with matcher.group(index) but this doesnt work if the event is like Veteran's Day since it is two words. I am only able to get the first word after 'when is'
我可以使用 matcher.group(index) 获取事件,但是如果事件类似于退伍军人节,则这不起作用,因为它是两个词。我只能得到“when is”之后的第一个词
What regex should I use to get all of the words after 'when is'
我应该使用什么正则表达式来获取“when is”之后的所有单词
Also, lets say I want to capture someones bday like
另外,假设我想捕捉某人的生日
'when is * birthday
'什么时候 * 生日
How do I capture all of the text between is and birthday with regex?
如何使用正则表达式捕获 is 和生日之间的所有文本?
回答by strmstn
You can express this as a character class and include spaces in it: when is ([\w ]+)
.
您可以将其表示为字符类并在其中包含空格:when is ([\w ]+)
。
回答by roydukkey
You could try this:
你可以试试这个:
^when is (.*)$
This will find a string that starts with when is
and capture everything else to the end of the line.
这将找到一个以开头的字符串when is
并将其他所有内容捕获到行尾。
The regex will return one group. You can access it like so:
正则表达式将返回一组。您可以像这样访问它:
String line = "when is Veteran's Day.";
Pattern pattern = Pattern.compile("^when is (.*)$");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.println("group 1: " + matcher.group(1));
System.out.println("group 2: " + matcher.group(2));
}
And the output should be:
输出应该是:
group 1: when is Veteran's Day.
group 2: Veteran's Day.
回答by Tripp Kinetics
\w
only includes word characters, which doesn't include spaces. Use [\w ]+
instead.
\w
只包含单词字符,不包含空格。使用[\w ]+
来代替。
回答by merlin2011
If you want to allow whitespace to be matched, you should explicitly allow whitespace.
如果要允许匹配空格,则应明确允许空格。
([\w\s]+)
However, roydukkey's solution will work if you want to capture everythingafter when is
.
但是,如果你想捕捉roydukkey的解决方案将工作一切之后when is
。
回答by tmanion
Don't use regular expressions when you don't need to!! Although the theory of regular expressions is beautiful in the thought that you can have a string do code operations for you, it is very memory inefficient for simple use cases.
不需要时不要使用正则表达式!!虽然正则表达式的理论很美,因为你可以让一个字符串为你做代码操作,但对于简单的用例来说,它的内存效率非常低。
If you are trying to get the word after "when is" ending by a space, you could do something like this:
如果你想在“when is”之后得到一个空格结尾的词,你可以这样做:
String start = "when is ";
String end = " ";
int startLocation = fullString.indexOf(start) + start.length();
String afterStart = fullString.substring(startLocation, fullString.length());
String word = afterStart.substring(0, afterStart.indexOf(end));
If you know the last word is Day, you can just make end = "Day" and add the length of that string of where to end the second substring.
如果您知道最后一个单词是 Day,则只需使 end = "Day" 并添加该字符串的长度以结束第二个子字符串。