Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多行之间的Java正则表达式子字符串_Java_Regex_Substring - Fatal编程技术网

多行之间的Java正则表达式子字符串

多行之间的Java正则表达式子字符串,java,regex,substring,Java,Regex,Substring,我有如下内容 c\cert\ "test1" text --Begin Cert cert content1 cert content 2 --End Cert c\cert\ "testCert2" text --Begin Cert cert test content1 cert test content 2 --End Cert c\cert\ "sampleCert2" text --Begin Cert sample content1 sample test content 2 -

我有如下内容

c\cert\ "test1" text
--Begin Cert
cert content1
cert content 2
--End Cert

c\cert\ "testCert2" text
--Begin Cert
cert test content1
cert test content 2
--End Cert

c\cert\ "sampleCert2" text
--Begin Cert
sample content1
sample test content 2
--End Cert
我需要提取内容并保存在地图中,如

Key:test1
value:"--Begin Cert
    cert content1
    cert content 2
    --End Cert"
Key:testCert2
value:"--Begin Cert
    cert test content1
    cert test content 2
    --End Cert"
. 
.
etc
我可以一行一行地循环。但我想用正则表达式。 这就是我尝试过的

Matcher m = Pattern.compile("(?m)^c\\\\cert\\\\ \"(\\w++)\" text\r?\n(.*?)\\s*$").matcher(configContent)
while (m.find()) {
map.put(m.group(1),m.group(2));
}

但我没有得到预期的结果。请帮助我形成正确的正则表达式。

您需要再次转义所有的
\
,因为java字符串,但正如Stribizev所说,如果您想匹配
\
,那么您需要正则表达式中的
\
,而java正则表达式中的
\\\\

您可能想要更像这样的东西:

(?m)c\\\\cert\s”(\\w++)\\stext\\s((?:.++\\n)+(?:.+)

所以这部分
(?m)c\\\\cert\s“(\\w++”)\\stext\\s
得到了引号中的内容,大部分是你的东西

这一点呢
((?:.+\\n)+(?:.+)

将捕获至少包含1个字符的任意行数,以下代码将执行此操作:

Pattern p = Pattern.compile("^c\\\\cert\\\\ \"([^\"]+)\" text\r?\n" +
                            "(--Begin Cert\r?\n.*?\r?\n--End Cert)[\r\n]*",
                            Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = p.matcher(input);
while (m.find()) {
    System.out.println("Key:" + m.group(1));
    System.out.println("value:\"" + m.group(2) + "\"");
    System.out.println();
}
运行时使用:

String input = "c\\cert\\ \"test1\" text\r\n" +
               "--Begin Cert\r\n" +
               "cert content1\r\n" +
               "cert content 2\r\n" +
               "--End Cert\r\n" +
               "\r\n" +
               "c\\cert\\ \"testCert2\" text\r\n" +
               "--Begin Cert\r\n" +
               "cert test content1\r\n" +
               "cert test content 2\r\n" +
               "--End Cert\r\n" +
               "\r\n" +
               "c\\cert\\ \"sampleCert2\" text\r\n" +
               "--Begin Cert\r\n" +
               "sample content1\r\n" +
               "sample test content 2\r\n" +
               "--End Cert\r\n";
你会得到:

键:test1
值:“--开始证书
证书内容1
证书内容2
--结束证书“
密钥:testCert2
值:“--开始证书
证书测试内容1
证书测试内容2
--结束证书“
密钥:sampleCert2
值:“--开始证书
样本内容1
样本测试内容2
--结束证书“

只将输入更改为换行符(
\n
而不是
\r\n
),它仍然可以工作。

要匹配反斜杠,正则表达式字符串中需要4个反斜杠-
\\
。您可以安全地删除第二个
^
,并将第一个
$
替换为
\r?\n
。你忘了带引号了。@Stribizev谢谢你的更正。我几乎可以得到我所期望的。但我只在m组(2)中获得“开始证书”。我需要得到“--\nBegin Cert\ncert content1\ncert CONTENT2\n--End Cert”,我想你得到了一个答案:)@Ramesh正则表达式中的
字符不包括换行符,看看这个正则表达式
(?:。+\\n)+(?:。+)