找到从特殊字符java开始的单词 [英] Find the words start from a special character java

查看:122
本文介绍了找到从特殊字符java开始的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在java中的字符串中找到以#符号开头的单词。标志和单词之间也可以有空格。

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.

字符串hi #how are#you将输出为:

how
you

我用regex尝试了这个,但仍然找不到合适的模式。请帮帮我。

I have tried this with regex, but still could not find a suitable pattern. Please help me on this.

谢谢。

推荐答案

使用#\ * *(\ w +) as你的正则表达式。

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));
}

这将打印出来:

how
you

这篇关于找到从特殊字符java开始的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆