给定下面的示例文本,我试图生成一个正则表达式,它将选择set private-key
和-----END ENCRYPTED PRIVATE KEY-----"
之间的三个部分,包括set private-key
和-----END ENCRYPTED PRIVATE KEY-----"
行。
我有表达式^.*set private-key.*$
,它选择了第一个部分,然后我有*^-----END ENCRYPTED PRIVATE KEY-----"$
,它当然选择了结束部分。我不知道的是如何将它们组合起来选择整个部分。
我尝试过^.*set private-key.*$[\s\S]*^-----END ENCRYPTED PRIVATE KEY-----"$
,但最终选择了它找到的第一个set private-key
和最后一个-----END ENCRYPTED PRIVATE KEY-----"
之间的所有内容,以及它们之间的所有内容。我想选择每个部分,因此在下面的示例中将选择3个部分。
我一直在网上摆弄一些正则表达式实验室,但似乎无法找到这个难题的解决方案,因此我在这里。
谢谢你。
丹
set comments "This is a comment "
set private-key "-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIpQqr96DiyZICAggA
MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIpQqr96DiyZICAggA
MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIpQqr96DiyZICAggA
-----END ENCRYPTED PRIVATE KEY-----"
set range global
set source factory
set last-updated 1546857127
next
edit "nothing important"
set password ENC b1yM5KXgCoMcQ5Nc8zXW4RUgQe2XnxnVw3JDavLkx7L/dMwEudi2CuZDVEnqaoP/K3oKQb7r0WMwblzPu3sBzJkrSP28WQ==
set comments "This is another comment"
set private-key "-----BEGIN ENCRYPTED PRIVATE KEY-----
JzW9TfmKu1lfYq/edslhke7RVPRpJ/Xu+Asbwd/P1qn/ffGbv713Xtyy95zBQbRe
8u1I3cGvr8LMHsMOdgFuHMr0OcVp7B9amCKQeXQImXK1SXHnZjPuIw7IYBlX1jvN
MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIpQqr96DiyZICAggA
-----END ENCRYPTED PRIVATE KEY-----"
next
edit "nothing important"
set password ENC b1yM5KXgCoMcQ5Nc8zXW4RUgQe2XnxnVw3JDavLkx7L/dMwEudi2CuZDVEnqaoP/K3oKQb7r0WMwblzPu3sBzJkrSP28WQ==
set comments "This is another comment"
set private-key "-----BEGIN ENCRYPTED PRIVATE KEY-----
JzW9TfmKu1lfYq/edslhke7RVPRpJ/Xu+Asbwd/P1qn/ffGbv713Xtyy95zBQbRe
8u1I3cGvr8LMHsMOdgFuHMr0OcVp7B9amCKQeXQImXK1SXHnZjPuIw7IYBlX1jvN
MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIpQqr96DiyZICAggA
-----END ENCRYPTED PRIVATE KEY-----"
edit "nothing important"
set password ENC b1yM5KXgCoMcQ5Nc8zXW4RUgQe2XnxnVw3JDavLkx7L/dMwEudi2CuZDVEnqaoP/K3oKQb7r0WMwblzPu3sBzJkrSP28WQ==
set private-key "-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDyqjjtjF
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDyqjjtjF
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDyqjjtjF
XUq7T96YiMJWNG4w==
-----END OPENSSH PRIVATE KEY-----
"
1条答案
按热度按时间bvjxkvbb1#
你很接近了!你需要做的就是让你的
[\s\S]*
非贪婪[\s\S]*?
。因此,它不会先消耗所有内容,然后将多个有效匹配组合成一个。请参阅此处的测试用例:https://regex101.com/r/MYZYGR/1