I would like to remove every # character if this character is the first letter of a word.
Here : hi, #I #want to remove# first #letter
Would become : hi, I want to remove# first letter
I tried this regex : ^# (?!$) but it only deletes # if it's the first letter of the whole string.
CodePudding user response:
You may try doing a regex replacement on (?<!\S)#
:
String input = "hi, #I #want to remove# first #letter";
String output = input.replaceAll("(?<!\\S)#", "");
System.out.println(output); // hi, I want to remove# first letter
CodePudding user response:
answer:
#\b. ?\b
can match the word start with #
explain:
# is the start
\b means the word start or end, here means word start
. ? means some charactors, the ? means must have more than one charactor
\b here means word end