Extract hash tag from String [closed]

瘦欲@ 提交于 2019-12-04 19:10:28

问题


I want to extract any words immediately after the # character in a String, and store them in a String[] array.

For example, if this is my String...

"Array is the most #important thing in any programming #language"

Then I want to extract the following words into a String[] array...

"important"
"language"

Could someone please offer suggestions for achieving this.


回答1:


try this -

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));
}

out put -

important
any
7
& 



回答2:


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));
}

The regex used is:

#      - A literal #
(      - Start of capture group
  \\w+ - One or more word characters
)      - End of capture group



回答3:


Try this regular expression

#\w+


来源:https://stackoverflow.com/questions/10432543/extract-hash-tag-from-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!