0

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

Athus Vieira
  • 364
  • 3
  • 13
  • Apparently you don't know where to start with your regex. Please check out [Reference - What does this regex mean resource](https://stackoverflow.com/questions/22937618), and [Learning Regular Expressions}(https://stackoverflow.com/questions/4736) for more info on regex. – Christian Baumann Sep 28 '20 at 14:42
  • It is not quite clear what rules the regex should follow. Try `(?:^|\b(and|or)\s+)(\w[\w.]*)\s+(!?=)\s+(\w+)`. See [this demo](https://regex101.com/r/vEqM70/1). – Wiktor Stribiżew Sep 28 '20 at 14:44

1 Answers1

0

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
Ryszard Czech
  • 10,599
  • 2
  • 12
  • 31