I want to find the words that start with a "#" sign in a string in java. There can be spaces between the sign and the word as well.
The string "hi #how are # you"
shall give the output as :
how
you
I have tried this with regex, but still could not find a suitable pattern. Please help me on this.
Thanks.
Use #\s*(\w+)
as your regex.
String yourString = "hi #how are # you";
Matcher matcher = Pattern.compile("#\\s*(\\w+)").matcher(yourString);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
This will print out:
how
you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With