How to match a pattern after a pattern in regex, using groups, negations, lookarounds

Viewed 31

I have a pattern " -[^ ]* ", the quotes are here to show the spaces. I want to match everything after it until this pattern again is encountered

something like this

(?<= -[^ ]* )[^( -[^ ]* )]*

or in a compact form

p = ( -[^ ]* )

(?<=p)[^p]*

the problem is that i cannot use * inside (?<= -[^ ]* ) statement and i cannot use () in [^( -[^ ]* )]. In both cases these characters are treated literally

1 Answers

[^( -[^ ]* )]* is negated character class, it matches characters different from round brackets, spaces, hyphen, caret, and so on.

Use

-[^ ]* ((?:(?! -[^ ]* ).)*)

See proof

Explanation

--------------------------------------------------------------------------------
  -                        '-'
--------------------------------------------------------------------------------
  [^ ]*                    any character except: ' ' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
                           ' '
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
         -                       ' -'
--------------------------------------------------------------------------------
        [^ ]*                    any character except: ' ' (0 or more
                                 times (matching the most amount
                                 possible))
--------------------------------------------------------------------------------
                                 ' '
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \1
Related