从String中提取哈希标记
我想在String
的#
字符后立即提取任何单词,并将它们存储在String[]
数组中。
例如,如果这是我的String
…
"Array is the most #important thing in any programming #language"
然后我想将以下单词提取到String[]
数组中……
"important" "language"
有人可以提供实现这一目标的建议。
尝试这个 –
String str="#important thing in #any programming #7 #& "; Pattern MY_PATTERN = Pattern.compile("#(\\S+)"); Matcher mat = MY_PATTERN.matcher(str); List strs=new ArrayList (); while (mat.find()) { //System.out.println(mat.group(1)); strs.add(mat.group(1)); }
出来 –
important any 7 &
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)); }
使用的正则表达式是:
# - A literal # ( - Start of capture group \\w+ - One or more word characters ) - End of capture group
试试这个正则表达式
#\w+