从String中提取哈希标记

bar*_*fty 9 java extract

我想#在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)

  • 此正则表达式不适用于 UTF-8,请勿在实际项目中使用它!:) (2认同)

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)


jue*_*n d 5

试试这个正则表达式

#\w+
Run Code Online (Sandbox Code Playgroud)