Search code examples
regexregex-group

Regex to separate groups of key, operator and value of a clause


What is the best regex to get groups of keys, operator and values from a clause like the image below?

What I have done so far is not accurate and is only able to get the first group: (^.*?(=|!=)+([^.]*))

enter image description here


Solution

  • Use

    (\w+(?:\.\w+)*)\s*(!=|=)\s*(\w+)
    

    See proof

    Explanation

    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                 more times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        (?:                      group, but do not capture (0 or more
                                 times (matching the most amount
                                 possible)):
    --------------------------------------------------------------------------------
          \.                       '.'
    --------------------------------------------------------------------------------
          \w+                      word characters (a-z, A-Z, 0-9, _) (1
                                   or more times (matching the most
                                   amount possible))
    --------------------------------------------------------------------------------
        )*                       end of grouping
    --------------------------------------------------------------------------------
      )                        end of \1
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \2:
    --------------------------------------------------------------------------------
        !=                       '!='
    --------------------------------------------------------------------------------
       |                        OR
    --------------------------------------------------------------------------------
        =                        '='
    --------------------------------------------------------------------------------
      )                        end of \2
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \3:
    --------------------------------------------------------------------------------
        \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                 more times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
      )                        end of \3