Search code examples
regexregex-group

Regex search for pattern and return group


I have a text to search in like this:

process ADT {
    { DOMAINNAME localdomain }
    { EODEFAULT {} }
    { EOXLATE {} }
    { XLATESTARTMODE 0 }
} 
process ORU {
    { DOMAINNAME localdomain }
    { EODEFAULT {} }
    { EOXLATE {} }
    { SMATCYCLESIZE 100 }
    { TRACINGDEFAULT 0 }
    { XLATE {
    { XLATESTARTMODE 0 }
}

My regex pattern is:

^process \w* {

With this I found two matches:

  • process ADT {
  • process ORU {

That result it near what I want. I want to get the whole text until the next match:

Match 1:

process ADT {
    { DOMAINNAME localdomain }
    { EODEFAULT {} }
    { EOXLATE {} }
    { XLATESTARTMODE 0 }
} 

And Match 2:

process ORU {
    { DOMAINNAME localdomain }
    { EODEFAULT {} }
    { EOXLATE {} }
    { SMATCYCLESIZE 100 }
    { TRACINGDEFAULT 0 }
    { XLATE {
    { XLATESTARTMODE 0 }
} 

How can I realize that?


Solution

  • Here is a pure regex solution which seems to be working:

    ^process \w+ \{.*?(?<=\n)\}(?!\S)
    

    This matches each process block, one at a time, with the closing \} being defined as preceded by newline and followed also by newline or the end of the string.

    Explanation:

    • ^ from the start of the string
    • process match literal process
    • space
    • \w+ process name
    • space
    • \{ opening bracket
    • .*? then match all content, across newlines, until reaching the nearest
    • (?<=\n)\} closing bracket, preceded by newline
    • (?!\S) and followed by newline or end of the string.

    Note: If your regex tool does not support dot all mode, then use this version to match across newlines:

    ^process \w+ \{[\s\S]*?(?<=\n)\}(?!\S)
    

    Here is a link to working regex demo.