Search code examples
regexgrok

Regular expression for untypical parentheses


Example input:

Ala ma kota (ASD defect - 7) kot ma ale.

Ideal output:

(ASD defect - 7)

How to parse this text from parentheses?


Solution

  • In Grok, you'd need the following regex with a named capture group:

    \((?<name>[^()]*)\)
    

    This will match a text inside parentheses excluding parentheses. To include them, just put them into the capturing group:

    (?<name>\([^()]*\))
    

    The negated character class [^()]* matches 0 or more characters other than ) and (.

    UPDATE:

    As for using nested capturing groups, here is an example:

    (?<sth2>\bwid:\s*(?<wid>\d+))
    

    enter image description here