Extract the content in parentheses

Regex can solve the problem of extracting the contents of non-nested brackets

I encountered a problem, that is, I need to extract the content in each square bracket in the string. I searched on the Internet and found that using regular expressions

(\[[^\]]*\])

The content in square brackets can be extracted, and the following text is used as the matching object:

PerformanceManager[1st square bracket]Product[2nd square bracket]<[3rd square bracket]79~

The matching result is:

0-->1st square bracket
1-->2nd square bracket
2-->3rd square bracket

(Can be tested with tools, URL: http://tools.jb51.net/regex/javascript )

Text processing with nested parentheses

Difficulty escalation: If the square brackets also contain square brackets, the regular expression will lose its effect, and you have to find another way to do it yourself and have enough food and clothing.

The AutoHotkey code is as follows:

msg := "PerformanceManager[第1个中括号]Product[第2个中括号[中括号中包含中括号]]<[第3个中括号]79~"

for k,v in ExtractMessage(msg)
  MsgBox % k "-->" v

return

  /**
   * 提取中括号中内容,忽略中括号中的中括号
   * @param msg
   * @return
   */    
ExtractMessage(_msg) {
    list := []
    start := 1
    startFlag := 0
    endFlag := 0
    msg:=StrSplit(_msg)
    ; OutputDebug % obj2str(msg)
    loop % msg.length()
    {
      i:=A_Index
      if (msg[i] = "[") {
        startFlag+=1
        OutputDebug % "startFlag =" startFlag
        if (startFlag = endFlag + 1) {
          start := i
          
          OutputDebug % "start=" start
        }
      } else if (msg[i] = "]") {
        endFlag+=1
        OutputDebug % "endFlag =" endFlag
        if (endFlag = startFlag) {
          OutputDebug % "endFlag=" endFlag
          list.Insert(SubStr(_msg,start + 1, i-start-1))
        }
      }
      
    }
    return list
  }

The output is as follows:

0-->1st square bracket
1-->2nd square bracket [square brackets contain square brackets]
2-->3rd square bracket

Algorithm principle

The main idea is to traverse the string, mark and count the start and end positions of the square brackets. If it is the end position corresponding to the start position of the square brackets, then the counts of the start position and the end position are the same, so what is intercepted is a complete The contents of the brackets.

Guess you like

Origin blog.csdn.net/liuyukuan/article/details/129166546