3

我知道如何标记字符串,但问题是我想标记如下所示。

String st = "'test1, test2','test3, test4'";

我尝试过的如下:

st.split(",");

这给了我输出:

'test1
test2'
'test3
test4'

但我希望输出为:

'test1, test2'
'test3, test4'

我该怎么做呢?

4

2 回答 2

3

Since single quotes are not mandatory, split will not work, because Java's regex engine does not allow variable-length lookbehind expressions. Here is a simple solution that uses regex to match the content, not the delimiters:

String st = "'test1, test2','test3, test4',test5,'test6, test7',test8";
Pattern p = Pattern.compile("('[^']*'|[^,]*)(?:,?)");
Matcher m = p.matcher(st);
while (m.find()) {
    System.out.println(m.group(1));
}

Demo on ideone.

You can add syntax for escaping single quotes by altering the "content" portion of the quoted substring (currently, it's [^']*, meaning "anything except a single quote repeated zero or more times).

于 2013-05-06T16:36:03.713 回答
1

最简单可靠的解决方案是使用 CSV 解析器。也许Commons CSV会有所帮助。

它将根据 CSV 规则对字符串进行转义。所以 even''可以在值内使用而不会破坏它。

示例代码如下: ByteArrayInputStream baos = new ByteArrayInputStream("'test1, test2','test3, test4'".getBytes());

    CSVReader reader = new CSVReader(new InputStreamReader(baos), ',', '\'');

    String[] read = reader.readNext();
    System.out.println("0: " + read[0]);
    System.out.println("1: " + read[1]);

    reader.close();

这将打印:

0: test1, test2
1: test3, test4

如果您使用 maven,您可以只导入依赖项:

    <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.0</version>
    </dependency>

并开始使用它。

于 2013-05-06T16:32:17.557 回答