java 正则表达式从字符串中获取未知长度的前两个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4209734/
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
Regex to get first two words of unknown length from a string
提问by TookTheRook
Say I have a string with various words of unknown length. I plan to split the string by using a regular expression. Something like:
假设我有一个字符串,其中包含各种长度未知的单词。我计划使用正则表达式拆分字符串。就像是:
String resString = origString.split(".*//s.*//s")[0];
What would be the regular expression to get the first two words? I was thinking .*//s.*//s
, so all characters, followed by a space, then all characters, followed by another space. But using that gives me the exact same string I had before. Am I going about this the wrong way?
获取前两个单词的正则表达式是什么?我在想.*//s.*//s
,所以所有字符,后跟一个空格,然后是所有字符,然后是另一个空格。但是使用它给了我与以前完全相同的字符串。我会以错误的方式解决这个问题吗?
Any help would be appreciated!
任何帮助,将不胜感激!
回答by icyrock.com
If you have only spaces between words, split by \\s+
. When you split, the array would be the words themselves. First two would be in arr[0]
and arr[1]
if you do:
如果单词之间只有空格,请按\\s+
. 拆分时,数组将是单词本身。前两个将在arr[0]
,arr[1]
如果你这样做:
String[] arr = origString.split("\s+");
回答by SuperJulietta
With regular expressions you can do something like this:
使用正则表达式,您可以执行以下操作:
public static ArrayList<String> split2(String line, int n){
line+=" ";
Pattern pattern = Pattern.compile("\w*\s");
Matcher matcher = pattern.matcher(line);
ArrayList<String> list = new ArrayList<String>();
int i = 0;
while (matcher.find()){
if(i!=n)
list.add(matcher.group());
else
break;
i++;
}
return list;
}
if you want the first n words, or simply this:
如果你想要前 n 个单词,或者只是这个:
public static String split3(String line){
line+=" ";
Pattern pattern = Pattern.compile("\w*\s\w*\s");
Matcher matcher = pattern.matcher(line);
matcher.find();
return matcher.group();
}
if you want only the first and second words.
如果你只想要第一个和第二个词。
回答by Bubba
If you want to split it on exactly the space character:
如果您想将其完全拆分为空格字符:
String[] parts = args[i].split(" ");
If you want to split it on any whitespace character (space, tab, newline, cr):
如果要将其拆分为任何空白字符(空格、制表符、换行符、cr):
String[] parts = args[i].split("\s");
To treat multiple adjacent spaces as one separator:
将多个相邻空格视为一个分隔符:
String[] parts = args[i].split(" +");
Same for whitespace:
空格相同:
String[] parts = args[i].split("\s+");
The first two words would be parts[0]
and parts[1]
前两个词是parts[0]
andparts[1]
回答by buru
Assuming your "words" consist of alphanumeric characters, the following regex will match the first 2 words:
假设您的“单词”由字母数字字符组成,以下正则表达式将匹配前 2 个单词:
\w+\s+\w+
回答by Ashok Reddy M
Use below method.
使用下面的方法。
public static String firstLettersOfWord(String word) {
StringBuilder result = new StringBuilder();
String[] myName = word.split("\s+");
for (String s : myName) {
result.append(s.charAt(0));
}
return result.toString();
}}