简体   繁体   English

Java字符串 - 获取(但不包括)两个正则表达式之间的所有内容?

[英]Java string - get everything between (but not including) two regular expressions?

In Java, is there a simple way to extract a substring by specifying the regular expression delimiters on either side, without including the delimiters in the final substring? 在Java中,是否有一种通过在任一侧指定正则表达式分隔符来提取子字符串的简单方法,而不包括最终子字符串中的分隔符?

For example, if I have a string like this: 例如,如果我有这样的字符串:

<row><column>Header text</column></row>

what is the easiest way to extract the substring: 提取子字符串的最简单方法是什么:

Header text

Please note that the substring may contain line breaks... 请注意,子字符串可能包含换行符...

thanks! 谢谢!

Write a regex like this: 写这样的正则表达式:

"(regex1)(.*)(regex2)"

... and pull out the middle group from the matcher (to handle newlines in your pattern you want to use Pattern.DOTALL ). ...并从匹配器中拉出中间组(处理模式中想要使用Pattern.DOTALL的换行符)。

Using your example we can write a program like: 使用您的示例,我们可以编写如下程序:

package test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {

    public static void main(String[] args) {
        Pattern p = Pattern.compile(
                "<row><column>(.*)</column></row>",
                Pattern.DOTALL
            );

        Matcher matcher = p.matcher(
                "<row><column>Header\n\n\ntext</column></row>"
            );

        if(matcher.matches()){
            System.out.println(matcher.group(1));
        }
    }

}

Which when run prints out: 哪个在运行时打印出来:

Header


text

You should not use regular expressions to decode XML - this will eventually break if the input is not strictly controlled. 您不应该使用正则表达式来解码XML - 如果输入没有严格控制,这最终会破坏。

The easiest thing is probably to parse the XML up in a DOM tree (Java 1.4 and newer contain a XML parser directly) and then navigate the tree to pick out what you need. 最简单的事情可能是在DOM树中解析XML(Java 1.4和更新版本直接包含XML解析器),然后导航树以选择您需要的内容。

Perhaps you would like to tell what you want to accomplish with your program? 也许你想告诉你想用你的程序完成什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM