作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我该怎么做?我想要一个像这样的字符串
*test_text*
被识别为模式字符串,但是像这样的字符串:
*test____text*
或
*test_*[text*
或者 *test_again_text*
不被认可
最佳答案
我猜想这里可能需要这个表达式:
(?i)^\*[a-z0-9]+_[a-z0-9]+\*$
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class re{
public static void main(String[] args){
final String regex = "(?i)^\\*[a-z0-9]+_[a-z0-9]+\\*$";
final String string = "*test_text*\n"
+ "*test____text*\n"
+ "*test_*[text*\n"
+ "*test_again_tex";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Full match: *test_text*
<小时/>
如果您想简化/修改/探索表达式,regex101.com 的右上角面板已对此进行了解释。 。如果您愿意,也可以在 this link 观看,它如何与一些示例输入相匹配。
<小时/>关于Java 正则表达式 : check is a string has specific structure: *test_text*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931548/
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 3 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!