我需要获取包含此文本的所有字符串 %variables% 这里有一些文字 %/variables%
示例
%enca% something here %variables% take this text %/variables%
other stuffs here, I dont need this
%variables% I need this too %/variables%
other stuffs, etc
我拥有的是这样的:
我尝试这样做:
%变量%(.*?)%/变量%
就像这样工作(只有一场比赛)
但在 Java 中不起作用:
private boolean variablesTag(String s)
{
Pattern pattern = Pattern.compile("/%variables%(.*?)%/variables%/gs");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
//do some stuff...stored, work with the string, etc...
};
return true;
}
如果您能告诉我如何将绳子放入内部,我将非常感激。 我想要的是这样的:
接受这段文字 这也是
我正在使用 NetBeans...
解决方案
Pattern pattern = Pattern.compile("%variables%(.*?)%/variables%",Pattern.MULTILINE|Pattern.DOTALL);
没有标志不起作用
最佳答案
在Java中,你不需要正则表达式上的“/”分隔符,事实上使用它们是不正确的。如果您想向正则表达式添加标志,可以使用 Pattern.compile
的两个参数版本(请参阅 API docs )。
改变
Pattern pattern = Pattern.compile("/%variables%(.*?)%/variables%/gs");
例如:
Pattern pattern = Pattern.compile("%variables%(.*?)%/variables%", Pattern.DOTALL);
然后在循环内使用 matcher.group(1)
访问捕获的内容。
关于java - 正则表达式 获取所有索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778877/