我正在嘗試決議 F5 物件。它具有以下結構。
Some Garbage text
ltm virtual The_Name_51244_sdfasfdasd {
address-status yes
enabled
fallback-persistence none
profiles {
/Common/GLOBAL_PROFILE {
context all
}
/Common/http {
context all
}
}
rate-class none
rules {
/Common/X-F-F
}
log-profiles none
source-address-translation {
pool SOME-SNAT-POOL
type snat
}
source-port preserve
vlans {
Vlan1111
}
service-down-immediate-action none
service-policy none
source 0.0.0.0/0
}
barbage text
ltm virtual The_Object_51244 {
address-status yes
enabled
fallback-persistence none
profiles {
/Common/GLOBAL_PROFILE {
context all
}
/Common/http {
context all
}
}
rate-class none
rules {
/Common/X-F-F
}
log-profiles none
source-address-translation {
pool SOME-SNAT-POOL
type snat
}
source-port preserve
vlans {
Vlan2222
}
service-down-immediate-action none
service-policy none
source 0.0.0.0/0
}
Trailing garbage text
我的正則運算式是((ltm virtual) ([a-zA-Z0-9_-]*) {(.|\n)*?})
https://regex101.com/r/ATJZys/1
我要捕獲的是 ltm virtual 之后的名稱以及大括號之間的所有內容。但是,上面的正則運算式似乎在一開始就停止了大括號匹配。
如何擴展它以匹配直到下一個父組或匹配直到大括號平衡?
uj5u.com熱心網友回復:
您可以使用
((ltm virtual) ([a-zA-Z0-9_-]*) ({(?:[^{}] |\g<4>)*}))
請參閱正則運算式演示。
詳情:
(
- 第一組開始(ltm virtual)
- 第 2 組:ltm virtual
字串([a-zA-Z0-9_-]*)
- 第 3 組:零個或多個字母、數字、下劃線或連字符(可改寫為[\w-]*
)({(?:[^{}] |\g<4>)*})
- 第 4 組:{
,然后是一個或多個字符的零次或多次重復,而不是{
第}
4 組遞回,然后是一個}
字符
)
- 第 1 組結束。
在 Java 中,要在某個正則運算式模式之后匹配嵌套的花括號、括號或任何其他單個字符定界符(更準確地說是在模式的末尾),您可以使用
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main (String[] args) throws java.lang.Exception
{
Character markStart = '{';
Character markEnd = '}';
Boolean includeMarkers = true;
int offset = (includeMarkers ? -1 : 0);
String s = "Some Garbage text\r\nltm virtual The_Name_51244_sdfasfdasd {\r\n address-status yes\r\n enabled\r\n fallback-persistence none\r\n profiles {\r\n /Common/GLOBAL_PROFILE {\r\n context all\r\n }\r\n /Common/http {\r\n context all\r\n }\r\n }\r\n rate-class none\r\n rules {\r\n /Common/X-F-F\r\n }\r\n log-profiles none\r\n source-address-translation {\r\n pool SOME-SNAT-POOL\r\n type snat\r\n }\r\n source-port preserve\r\n vlans {\r\n Vlan1111\r\n }\r\n service-down-immediate-action none\r\n service-policy none\r\n source 0.0.0.0/0\r\n}\r\nbarbage text\r\nltm virtual The_Object_51244 {\r\n address-status yes\r\n enabled\r\n fallback-persistence none\r\n profiles {\r\n /Common/GLOBAL_PROFILE {\r\n context all\r\n }\r\n /Common/http {\r\n context all\r\n }\r\n }\r\n rate-class none\r\n rules {\r\n /Common/X-F-F\r\n }\r\n log-profiles none\r\n source-address-translation {\r\n pool SOME-SNAT-POOL\r\n type snat\r\n }\r\n source-port preserve\r\n vlans {\r\n Vlan2222\r\n }\r\n service-down-immediate-action none\r\n service-policy none\r\n source 0.0.0.0/0\r\n}\r\nTrailing garbage text";
Pattern patternBefore = Pattern.compile("(ltm\\s virtual)\\s ([\\w-] )\\s " Pattern.quote(markStart.toString()));
Matcher m = patternBefore.matcher(s);
while (m.find()) {
System.out.println("Group 1: " m.group(1));
System.out.println("Group 2: " m.group(2));
String res = getBalancedSubstring(s.substring(m.end()), markStart, markEnd, includeMarkers);
System.out.println("Found nested: " res "\n----");
if (res == null) {
System.out.println("No nested parens match found, this match must be failed.");
}
else {
s = s.substring(m.end() res.length() offset);
m.reset(s);
}
}
}
public static String getBalancedSubstring(String s, Character markStart, Character markEnd, Boolean includeMarkers)
{
int level = 1;
for (int i = 0; i < s.length(); i ) {
char c = s.charAt(i);
if (c == markStart) {
level ;
}
else if (c == markEnd) {
if (level == 1) {
return (includeMarkers ? markStart.toString() : "") s.substring(0, (includeMarkers ? i 1 : i));
}
if (level > 0) level--;
}
}
return null;
}
}
在線查看Java 演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/483129.html