Question: I have a long string let's say

```
I like this #computer and I want to buy it from #XXXMall.

```

I know the regular expression pattern is 

```
Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");

```

Now i want to get all the hashtags in an array. How can i use this expression to get array of all hash tags from string something like 

```
ArrayList hashtags = getArray(pattern, str)

```

Answer: 
You can write like?

```
private static List<String> getArray(Pattern tagMatcher, String str) {
    Matcher m = tagMatcher.matcher(str);
    List<String> l = new ArrayList<String>();
    while(m.find()) {
        String s = m.group(); //will give you "#computer"
        s = s.substring(1); // will give you just "computer"
        l.add(s);
    }
    return l;
}

```

Also you can use `\\w-` instead of `A-Za-z0-9-_` making the regex `[#]+[\\w]+\\b`
Here is one way, using [`Matcher`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html)

```
Pattern tagMatcher = Pattern.compile("#+[-\\w]+\\b");
Matcher m = tagMatcher.matcher(stringToMatch);

ArrayList<String> hashtags = new ArrayList<>();

while (m.find()) {
    hashtags.add(m.group());
}

```

I took the liberty of simplifying your regex. `#` does not need to be in a character class. `[A-Za-z0-9_]` is the same as `\w`, so `[A-Za-z0-9-_]` is the same as `[-\w]`