This one should be the way:
#(\w+)(?:[, .]|$)
#
matches#
literally\w
is a word with at least one letter(?:)
is non-capturing group[, .]|$
is set of ending characters including the end of line$
For more information check out Regex101.
In Java don’t forget to escape with double \\
:
String str = "hi #myname, you got #amount";
Matcher m = Pattern.compile("#(\\w+)(?:[, .]|$)").matcher(str);
while (m.find()) {
...
}
3
solved I want to find all words using java regex, that starts with “#” and ends with space or “.”