我有这种类型的子字符串
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 to get string between curly braces "{I want what's between the curly braces}"
但它只获取一对“{”和“}”之间的数据,而不计算第一个之后的“{”。
我没有完整的代码,但有一些如何进行的方向???
最佳答案
虽然使用 RegEx 并不能完美地完成此操作,但使用 stack 总是更好。为此。
但它只需要一个正则表达式解决方案就可以工作(并不总是):
(?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 «\}»
-->
关于java - 正则表达式查找 Java 中大括号内的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12399443/