Find the words start from a special character java

后端 未结 5 1655
梦毁少年i
梦毁少年i 2021-02-08 21:18

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 # yo

5条回答
  •  甜味超标
    2021-02-08 21:48

    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
    

提交回复
热议问题