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`
[This link](http://tutorials.jenkov.com/java-regex/matcher.html) would surely be helpful for achieving what you want.

It says:

> 
> The find() method searches for occurrences of the regular expressions
>  in the text passed to the Pattern.matcher(text) method, when the
>  Matcher was created. If multiple matches can be found in the text, the
>  find() method will find the first, and then for each subsequent call
>  to find() it will move to the next match.
> 
> 
> The methods start() and end() will give the indexes into the text
>  where the found match starts and ends.
> 
> 
> 

Example:

```
String text    =
        "This is the text which is to be searched " +
        "for occurrences of the word 'is'.";

String patternString = "is";

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

int count = 0;
while(matcher.find()) {
    count++;
    System.out.println("found: " + count + " : "
            + matcher.start() + " - " + matcher.end());
}

```

You got the hint now.