Pattern pattern = Pattern.compile("#([^#]+)#");
Matcher matcher = pattern.matcher("I #have# #a# #string#");
while(matcher.find()){
String y = matcher.group(1);
// do something with y
}
Explanation:
The parenthesis around [^#]+ allow to capture the content between them, and access it later by calling group(1) on the matcher. The 1 means first parenthesis group (in our example there is only one anyway).
The loop might be optional in your case. It iterates on all the words between # found in the string. If you only want the first one just call matcher.find() once with no loop.