Search code examples
regexmatchexpressionmatching

Match after a string and whitespaces?


If I have the following line, for example:

COLOR1=               Light Blue
COLOR2=    Dark Red

I'm looking for a regex that if I pass the string "COLOR1=" to it, it will match only "Light Blue", without all the whitespace before "Light". The same if I pass "COLOR2=", which should match only "Dark Red".

I tried (?<=COLOR1=)[^.]*, but it doesn't remove whitespace after the "=" and before the first non-empty character, and has problems with newlines.

EDIT: I am using the Nim programming language, which compiles to C code. Regular expressions in this language are based on PCRE (Perl-Compatible Regular Expressions) C library.


Solution

  • You can use \K to reset the match after matching the key, the equal sign and any subsequent whitespaces:

    COLOR1=\s*\K.*
    

    Demo: https://regex101.com/r/zSwjKO/1