Java 当前缀分隔符和后缀分隔符不同时,拆分字符串的最佳方法是什么?

Java 当前缀分隔符和后缀分隔符不同时,拆分字符串的最佳方法是什么?,java,xml,regex,string-split,Java,Xml,Regex,String Split,在Java中,当每个块开头的分隔符与每个块末尾的分隔符不同时,将字符串拆分为块数组的最佳方法是什么 例如,假设我有String=“abc 1234 xyz abc 5678 xyz” 我想应用某种复杂的拆分,以获得{“1234”,“5678”} 首先想到的是: String[] parts = string.split("abc"); for (String part : parts) { String[] blocks = part.split("xyz"); String d

在Java中,当每个块开头的分隔符与每个块末尾的分隔符不同时,将字符串拆分为块数组的最佳方法是什么

例如,假设我有
String=“abc 1234 xyz abc 5678 xyz”

我想应用某种复杂的
拆分
,以获得
{“1234”,“5678”}

首先想到的是:

String[] parts = string.split("abc");
for (String part : parts)
{
    String[] blocks = part.split("xyz");
    String data = blocks[0];
    // Do some stuff with the 'data' string
}
有没有更简单/更干净/更有效的方法

我的目的(您可能已经猜到)是解析XML文档

我想将给定的XML字符串拆分为给定标记的内部XML块

例如:

String xml = "<tag>ABC</tag>White Spaces Only<tag>XYZ</tag>";
String[] blocks = Split(xml,"<tag>","</tag>"); // should be {"ABC","XYZ"}
String xml=“ABCWhite Spaces OnlyXYZ”;
String[]blocks=Split(xml,“,”);//应该是{“ABC”,“XYZ”}
您将如何实现
String[]拆分(String str、String前缀、String后缀)

谢谢

您可以为这种类型的字符串编写正则表达式…

>像<代码> **((abc)”(XYZ S*ABC)(\s*XYZ $))S**/COD>在开始时说<代码> ABC < /代码>,或者结尾是“代码> XYZ < /代码>,还是在代码中间>代码> abc XYZ < /代码>(模空间)?这会在开始时产生一个空值,但除此之外,它似乎可以做您想要的事情

import java.util.Arrays;

public class RegexDelimitersExample {
    public static void main(String[] args) {
        final String string = "abc 1234 xyz abc 5678 xyz";
        final String pattern = "\\s*((^abc)|(xyz\\s*abc)|(\\s*xyz$))\\s*";
        final String[] parts_ = string.split( pattern );
        // parts_[0] is "", because there's nothing before ^abc,
        // so a copy of the rest of the array is what we want.
        final String[] parts = Arrays.copyOfRange( parts_, 1, parts_.length );
        System.out.println( Arrays.deepToString( parts ));
    }
}
根据处理空间的方式,可以根据需要进行调整。例如:

\s*((^abc)|(xyz\s*abc)|(\s*xyz$))\s*     # original
(^abc\s*)|(\s*xyz\s*abc\s*)|(\s*xyz$)    # no spaces on outside
...                                      # ...
…但您不应该将其用于XML。 不过,正如我在注释中所指出的,这将用于拆分具有这些分隔符的非嵌套字符串。您将无法使用正则表达式处理嵌套情况(例如,
abc 12345 xyz xyz
),因此您将无法处理常规XML(这似乎是您的意图)。如果您确实需要解析XML,请使用为XML设计的工具(例如,解析器、XPath查询等)。

您可以为这种类型的字符串编写正则表达式…

>像<代码> **((abc)”(XYZ S*ABC)(\s*XYZ $))S**/COD>在开始时说<代码> ABC < /代码>,或者结尾是“代码> XYZ < /代码>,还是在代码中间>代码> abc XYZ < /代码>(模空间)?这会在开始时产生一个空值,但除此之外,它似乎可以做您想要的事情

import java.util.Arrays;

public class RegexDelimitersExample {
    public static void main(String[] args) {
        final String string = "abc 1234 xyz abc 5678 xyz";
        final String pattern = "\\s*((^abc)|(xyz\\s*abc)|(\\s*xyz$))\\s*";
        final String[] parts_ = string.split( pattern );
        // parts_[0] is "", because there's nothing before ^abc,
        // so a copy of the rest of the array is what we want.
        final String[] parts = Arrays.copyOfRange( parts_, 1, parts_.length );
        System.out.println( Arrays.deepToString( parts ));
    }
}
根据处理空间的方式,可以根据需要进行调整。例如:

\s*((^abc)|(xyz\s*abc)|(\s*xyz$))\s*     # original
(^abc\s*)|(\s*xyz\s*abc\s*)|(\s*xyz$)    # no spaces on outside
...                                      # ...
…但您不应该将其用于XML。
不过,正如我在注释中所指出的,这将用于拆分具有这些分隔符的非嵌套字符串。您将无法使用正则表达式处理嵌套情况(例如,
abc 12345 xyz xyz
),因此您将无法处理常规XML(这似乎是您的意图)。如果您确实需要解析XML,请使用为XML设计的工具(例如,解析器、XPath查询等)。

这里不要使用正则表达式。但您也不必进行全面的XML解析。使用。在您的示例中要搜索的表达式为

//tag/text()
需要的代码是:

import org.w3c.dom.NodeList;
import org.xml.sax.*;
import javax.xml.xpath.*;

public class Test {

    public static void main(String[] args) throws Exception {

        InputSource ins = new InputSource("c:/users/ndh/hellos.xml");
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList list = (NodeList)xpath.evaluate("//bar/text()", ins, XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            System.out.println(list.item(i).getNodeValue());
        }

    }
}
import org.w3c.dom.NodeList;
导入org.xml.sax.*;
导入javax.xml.xpath.*;
公开课考试{
公共静态void main(字符串[]args)引发异常{
InputSource ins=newInputSource(“c:/users/ndh/hellos.xml”);
XPath=XPathFactory.newInstance().newXPath();
NodeList list=(NodeList)xpath.evaluate(“//bar/text()”,ins,XPathConstants.NODESET);
对于(int i=0;i
我的示例xml文件在哪里

<?xml version="1.0"?>
<foo>
    <bar>hello</bar>
    <bar>ohayoo</bar>
    <bar>hola</bar>
</foo>

你好
奥哈尤
你好

这是最具声明性的方法。

这里不要使用正则表达式。但您也不必进行全面的XML解析。使用。在您的示例中要搜索的表达式为

//tag/text()
需要的代码是:

import org.w3c.dom.NodeList;
import org.xml.sax.*;
import javax.xml.xpath.*;

public class Test {

    public static void main(String[] args) throws Exception {

        InputSource ins = new InputSource("c:/users/ndh/hellos.xml");
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList list = (NodeList)xpath.evaluate("//bar/text()", ins, XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            System.out.println(list.item(i).getNodeValue());
        }

    }
}
import org.w3c.dom.NodeList;
导入org.xml.sax.*;
导入javax.xml.xpath.*;
公开课考试{
公共静态void main(字符串[]args)引发异常{
InputSource ins=newInputSource(“c:/users/ndh/hellos.xml”);
XPath=XPathFactory.newInstance().newXPath();
NodeList list=(NodeList)xpath.evaluate(“//bar/text()”,ins,XPathConstants.NODESET);
对于(int i=0;i
我的示例xml文件在哪里

<?xml version="1.0"?>
<foo>
    <bar>hello</bar>
    <bar>ohayoo</bar>
    <bar>hola</bar>
</foo>

你好
奥哈尤
你好

这是最具声明性的方法。

最好使用一个专用的XML解析器。 请参阅关于Java的最佳XML解析器


我发现这是一个简单而好的方法。

最好使用一个专用的XML解析器。 请参阅关于Java的最佳XML解析器


我发现这是一个简单而好的解决方案。

我认为最好的解决方案是解析XML文件,这不是一行代码

下面是另一个问题的示例代码,用于解析文档,然后使用XPATH:

String xml = "<resp><status>good</status><msg>hi</msg></resp>";

InputSource source = new InputSource(new StringReader(xml));

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(source);

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

String msg = xpath.evaluate("/resp/msg", document);
String status = xpath.evaluate("/resp/status", document);

System.out.println("msg=" + msg + ";" + "status=" + status);
stringxml=“goodhi”;
InputSource=新的InputSource(新的StringReader(xml));
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document=db.parse(源代码);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xpathFactory.newXPath();
字符串msg=xpath.evaluate(“/resp/msg”,document);
字符串status=xpath.evaluate(“/resp/status”,文档);
System.out.println(“msg=“+msg+”;“+”status=“+status”);

这篇文章的完整线索

IMHO最好的解决方案是解析XML文件,这不是一行的事情

下面是另一个问题的示例代码,用于解析文档,然后使用XPATH:

String xml = "<resp><status>good</status><msg>hi</msg></resp>";

InputSource source = new InputSource(new StringReader(xml));

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(source);

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

String msg = xpath.evaluate("/resp/msg", document);
String status = xpath.evaluate("/resp/status", document);

System.out.println("msg=" + msg + ";" + "status=" + status);
stringxml=“goodhi”;
InputSource=新的InputSource(新的StringReader(xml));
DocumentBuilderFactory dbf=DocumentB