简体   繁体   English

正则表达式找出Java中花括号内的subtring

[英]Regex to find out a subtring which is inside curly braces in Java

I have this type of subtring 我有这种类型的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

so basically i have java class type of structure 所以基本上我有java类的结构类型
and now i want a piece of code which can get me following substrings(SS#) 现在我想要一段代码,可以让我跟随以下子字符串(SS#)
SS1: SS1:

        string 4
        string 5

SS2: SS2:

        string 7
        string 8

SS3: SS3:

            string 13
            string 14

SS4: SS4:

string 16
string 17

SS5: SS5:

        string 10
        string 11
        string 12
        {
            string 13
            string 14
        }
        string 15

SS6: 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
    }

so basically i want a piece of code that can get me various parts(function, classes, but not any loops) of string(java class) into different substrings... 所以基本上我想要一段代码可以让我将字符串(java类)的各个部分(函数,类,但没有任何循环)转换为不同的子字符串...
i read this 我读了这个
Regex to get string between curly braces "{I want what's between the curly braces}" 正则表达式获取大括号之间的字符串“ {我想大括号之间是什么}”
but it only gets me data between a pair of '{', and '}' without counting the '{' that have come after the first. 但它只会为我获取一对“ {”和“}”之间的数据,而没有计算第一个之后的“ {”。
i don't the full code, but some direction into how to proceed??? 我没有完整的代码,但如何进行一些指导???

Though this is not perfectly done using RegEx, it's always better to use a stack for that. 尽管使用RegEx不能完美完成此操作,但为此最好使用堆栈

But it only requires a RegEx solution then it might work (not always): 但是它只需要一个RegEx解决方案,然后就可以工作(并非总是如此):

(?is)\{[^}]*?\}(?=.*?\})

Explanation 说明

<!--

    (?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 «\}»
    -->

I dont know a regex for this but there is an another solution i can suggest for this: I'm just writing the sudo code: 我不知道这是一个正则表达式,但是我可以为此建议另一个解决方案:我只是在编写sudo代码:

  1. scan character of the given input string 给定输入字符串的扫描字符
  2. if char is { push char position to stack, 如果char是{将char位置推入堆栈,
  3. else if char is } pop position from stack and take substring(poped_postion, current_position) as SS# 否则,如果char是}堆栈中的弹出位置,并以substring(poped_postion,current_position)作为SS#
  4. goto 1 (scan next character until there's characters left in string) 转到1(扫描下一个字符,直到字符串中剩余字符为止)

It will be very difficult to do this with regular expressions. 使用正则表达式很难做到这一点。 I'd suggest that you split this strcture by new line and with a few simple rules to create a data structure of HashMap-s . 我建议您通过换行并用一些简单的规则来分割此结构,以创建HashMap-s的数据结构。 String 2 will be the key but it will have no value. 字符串2是键,但没有值。 String3 will be the next key and its value will be the thing in the curly braces below starting with row that begins with { and ending with line that begins with }. String3将是下一个键,其值将是下面的花括号中的东西,该花括号以以{开头的行和以}开头的行开头。

尝试使用此正则表达式来匹配{string}

\{(.*)\}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM