我想#在a中的字符后立即提取任何单词String,并将它们存储在String[]数组中.
例如,如果这是我的String......
"Array is the most #important thing in any programming #language"
Run Code Online (Sandbox Code Playgroud)
然后我想将以下单词提取到String[]数组中......
"important"
"language"
Run Code Online (Sandbox Code Playgroud)
有人可以提供实现这一目标的建议.
Sub*_*der 22
试试这个 -
String str="#important thing in #any programming #7 #& ";
Pattern MY_PATTERN = Pattern.compile("#(\\S+)");
Matcher mat = MY_PATTERN.matcher(str);
List<String> strs=new ArrayList<String>();
while (mat.find()) {
//System.out.println(mat.group(1));
strs.add(mat.group(1));
}
Run Code Online (Sandbox Code Playgroud)
出来 -
important
any
7
&
Run Code Online (Sandbox Code Playgroud)
cod*_*ict 13
String str = "Array is the most #important thing in any programming #language";
Pattern MY_PATTERN = Pattern.compile("#(\\w+)");
Matcher mat = MY_PATTERN.matcher(str);
while (mat.find()) {
System.out.println(mat.group(1));
}
Run Code Online (Sandbox Code Playgroud)
使用的正则表达式是:
# - A literal #
( - Start of capture group
\\w+ - One or more word characters
) - End of capture group
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8138 次 |
| 最近记录: |