[英]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.