作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下所示的字符串:
*to *make/to *have *a *dab *at *smb. (\*dictionary) some text without special symbols at the begging.
目标是从字符串中提取以 *
符号开头的单词,这是我的做法:
Matcher matcher = Pattern.compile("[*]\\s*(\\w+)").matcher(input);
while (matcher.find()) {
phrase += matcher.group(1) + " ";
}
输出如下:
to make have a dab at smb dictionary
我确实理解为什么不包含括号(它们与模式不匹配),但是有人可以向我解释为什么 /to
部分不匹配吗?
是否有办法增强我的模式,使其在匹配项周围包含特殊字符(如果存在)?
预期输出:
to make/to have a dab at smb. (dictionary)
最佳答案
您可以使用此正则表达式与 2 个捕获组来查找 *
之前和之后的所有非空白文本。
(\S*)\*\s*(\S+)
您需要组合.group(1)
一个.group(2)
里面while
循环。
对于 Java,请使用此正则表达式声明:
final String regex = "(\\S*)\\*\\s*(\\S+)";
关于java - 如何增强 Java 模式,使其在匹配项周围也包含特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45358410/
我是一名优秀的程序员,十分优秀!