Java RegEx SubString 多行之间
Java RegEx SubString Between multiple lines
我有如下内容。
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
我可以逐行循环。但我想用 RegEx 来做。
这是我试过的。
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 字符串,但也像 stribizhev 所说的,如果你想匹配 \
那么你需要 \
在正则表达式中,但 \\
在 java 正则表达式中。
您可能想要更多这样的东西:
(?m)c\\cert\\\s"(\w++)"\stext\s((?:.+\n)+(?:.+))
所以这部分 (?m)c\\cert\\\s"(\w++)"\stext\s
得到了引号中的内容,主要是你的东西 java-ified
还有这个
((?:.+\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";
你得到:
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"
Key:sampleCert2
value:"--Begin Cert
sample content1
sample test content 2
--End Cert"
仅将输入更改为换行符(\n
而不是 \r\n
),它仍然有效。
我有如下内容。
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
我可以逐行循环。但我想用 RegEx 来做。 这是我试过的。
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 字符串,但也像 stribizhev 所说的,如果你想匹配 \
那么你需要 \
在正则表达式中,但 \\
在 java 正则表达式中。
您可能想要更多这样的东西:
(?m)c\\cert\\\s"(\w++)"\stext\s((?:.+\n)+(?:.+))
所以这部分 (?m)c\\cert\\\s"(\w++)"\stext\s
得到了引号中的内容,主要是你的东西 java-ified
还有这个
((?:.+\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";
你得到:
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"
Key:sampleCert2
value:"--Begin Cert
sample content1
sample test content 2
--End Cert"
仅将输入更改为换行符(\n
而不是 \r\n
),它仍然有效。