我对Java (或任何语言)中的正则表达式都是新手,我想使用它们来做一个查找。我不知道该怎么做的棘手部分是替换字符串中匹配的内容。
例如,如果我要查找的代码行是
Person item6 [can {item thing [wrap]}]
我可以编写一个正则表达式来查找这一行,但是查找单词"thing“是什么(因为它在不同的行中可能不同)是我的问题。我可能希望将该单词替换为其他单词,或者将其保存在变量中以备后用。使用Java的regex引擎有什么简单的方法可以做到这一点吗?
发布于 2010-06-09 23:49:17
是。您将其包装在“捕获组”中,这只是正则表达式中与感兴趣的单词匹配的部分的一些()。
下面是一个示例:
public static void main(String[] args) {
Pattern pat = Pattern.compile("testing (\\d+) widgets");
String text = "testing 5 widgets";
Matcher matcher = pat.matcher(text);
if (matcher.matches()) {
System.out.println("Widgets tested : " + matcher.group(1));
} else {
System.out.println("No match");
}
}
Pattern和Matcher来自java.util.regex。String类中有一些快捷方式,但它们是最灵活的
发布于 2010-06-10 08:26:36
问题说明不是很清楚,但这里有一些想法可能会起作用:
使用lookarounds和replaceAll/First
下面的正则表达式匹配前面是字符串"{item "
,后面是字符串" ["
的\w+
。Lookaround仅用于精确匹配\w+
。元字符{
和[
根据需要进行转义。
String text =
"Person item6 [can {item thing [wrap]}]\n" +
"Cat item7 [meow meow {item thang [purr]}]\n" +
"Dog item8 [maybe perhaps {itemmmm thong [woof]}]" ;
String LOOKAROUND_REGEX = "(?<=\\{item )\\w+(?= \\[)";
System.out.println(
text.replaceAll(LOOKAROUND_REGEX, "STUFF")
);
这将打印:
Person item6 [can {item STUFF [wrap]}]
Cat item7 [meow meow {item STUFF [purr]}]
Dog item8 [maybe perhaps {itemmmm thong [woof]}]
参考文献
使用捕获组而不是查找
Lookaround应该被明智地使用。Lookbehinds特别是在Java中是非常有限的。一种更常用的技术是使用捕获组来匹配更多有趣的部分。
下面的正则表达式匹配前面的类似模式\w+
,但也包括"{item "
前缀和" ["
后缀。此外,item
中的m
可以无限制地重复(这是Java中无法匹配的)。
String CAPTURING_REGEX = "(\\{item+ )(\\w+)( \\[)";
System.out.println(
text.replaceAll(CAPTURING_REGEX, "$1STUFF$3")
);
这将打印:
Person item6 [can {item STUFF [wrap]}]
Cat item7 [meow meow {item STUFF [purr]}]
Dog item8 [maybe perhaps {itemmmm STUFF [woof]}]
我们的模式有3个捕获组:
(\{item+ )(\w+)( \[)
\________/\___/\___/
group 1 2 3
请注意,我们不能简单地用"STUFF"
替换匹配的内容,因为我们匹配了一些“无关”的部分。我们对替换它们不感兴趣,所以我们捕获这些部分并将它们放回替换字符串中。在Java语言中,我们引用替换字符串中捕获的组的方式是使用$
符号;因此在上面的示例中使用$1
和$3
。
参考文献
使用Matcher
获得更大的灵活性
并不是所有的事情都可以通过替换字符串来完成。例如,Java没有将捕获的字符串大写的后处理功能。在这些更一般的替换方案中,您可以使用如下所示的Matcher
循环:
Matcher m = Pattern.compile(CAPTURING_REGEX).matcher(text);
StringBuffer sb = new StringBuffer();
while (m.find()) {
System.out.println("Match found");
for (int i = 0; i <= m.groupCount(); i++) {
System.out.printf("Group %d captured <%s>%n", i, m.group(i));
}
m.appendReplacement(sb,
String.format("%s%s %<s and more %<SS%s",
m.group(1), m.group(2), m.group(3)
)
);
}
m.appendTail(sb);
System.out.println(sb.toString());
上面的打印结果:
Match found
Group 0 captured <{item thing [>
Group 1 captured <{item >
Group 2 captured <thing>
Group 3 captured < [>
Match found
Group 0 captured <{item thang [>
Group 1 captured <{item >
Group 2 captured <thang>
Group 3 captured < [>
Match found
Group 0 captured <{itemmmm thong [>
Group 1 captured <{itemmmm >
Group 2 captured <thong>
Group 3 captured < [>
Person item6 [can {item thing thing and more THINGS [wrap]}]
Cat item7 [meow meow {item thang thang and more THANGS [purr]}]
Dog item8 [maybe perhaps {itemmmm thong thong and more THONGS [woof]}]
参考文献
java.util.regex.Pattern
java.util.regex.Matcher
group(int)
-访问单独捕获的stringsappendReplacement
--不幸的是,StringBuffer
-only
java.util.Formatter
-在上述示例中的printf
和String.format
中使用附件
https://stackoverflow.com/questions/3010684
复制相似问题