Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract hash tag from String [closed]

Tags:

java

extract

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.

like image 614
baraaalsafty Avatar asked May 03 '12 13:05

baraaalsafty


2 Answers

Try this regular expression

#\w+
like image 170
juergen d Avatar answered Oct 16 '22 05:10

juergen d


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
like image 30
codaddict Avatar answered Oct 16 '22 07:10

codaddict