[英]Regex to find out a subtring which is inside curly braces in Java
我有這種類型的Subtring
string 1
{
string 2
string 3
{
string 4
string 5
}
string 6
{
string 7
string 8
}
string 9
{
string 10
string 11
string 12
{
string 13
string 14
}
string 15
}
}
string 16
string 17
所以基本上我有java類的結構類型
現在我想要一段代碼,可以讓我跟隨以下子字符串(SS#)
SS1:
string 4
string 5
SS2:
string 7
string 8
SS3:
string 13
string 14
SS4:
string 16
string 17
SS5:
string 10
string 11
string 12
{
string 13
string 14
}
string 15
SS6:
string 2
string 3
{
string 4
string 5
}
string 6
{
string 7
string 8
}
string 9
{
string 10
string 11
string 12
{
string 13
string 14
}
string 15
}
所以基本上我想要一段代碼可以讓我將字符串(java類)的各個部分(函數,類,但沒有任何循環)轉換為不同的子字符串...
我讀了這個
正則表達式獲取大括號之間的字符串“ {我想大括號之間是什么}”
但它只會為我獲取一對“ {”和“}”之間的數據,而沒有計算第一個之后的“ {”。
我沒有完整的代碼,但如何進行一些指導???
盡管使用RegEx不能完美完成此操作,但為此最好使用堆棧 。
但是它只需要一個RegEx解決方案,然后就可以工作(並非總是如此):
(?is)\{[^}]*?\}(?=.*?\})
說明
<!--
(?is)\{[^}]*?\}(?=.*?\})
Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s) «(?is)»
Match the character “{” literally «\{»
Match any character that is NOT a “}” «[^}]*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “}” literally «\}»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*?\})»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “}” literally «\}»
-->
我不知道這是一個正則表達式,但是我可以為此建議另一個解決方案:我只是在編寫sudo代碼:
SS#
使用正則表達式很難做到這一點。 我建議您通過換行並用一些簡單的規則來分割此結構,以創建HashMap-s的數據結構。 字符串2是鍵,但沒有值。 String3將是下一個鍵,其值將是下面的花括號中的東西,該花括號以以{開頭的行和以}開頭的行開頭。
嘗試使用此正則表達式來匹配{string}
,
\{(.*)\}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.