Extract hash tag from String

I want to extract any words immediately after the # character in 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 an array of String[] ...

 "important" "language" 

Can anyone suggest suggestions to achieve this.

+7
source share
3 answers

try it -

 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 & 
+19
source
 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)); } 

Regular expression used:

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

Try this regex

 #\w+ 
+5
source

All Articles