1

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.

Drackich
  • 17
  • 5
  • 2
    Which regular expression dialect are you using? That is, in which programming language (Perl, Python, Java, etc) or software application (Notepad++, Excel, ...) are you working? – Lover of Structure Feb 24 '23 at 04:35
  • `COLOR\d=\s+(.*)` works for me on both examples. The color names are in capture group 1. This will work for all strings that start with COLOR + a number followed by an equals sign. – Ken White Feb 24 '23 at 04:38
  • @LoverofStructure I am using the Nim programming language, which compiles to C code. – Drackich Feb 24 '23 at 15:32

2 Answers2

1

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

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Note that `\K` is not universally supported by regex engines. It is not supported, for example, by .NET, Java, Javascript or Python. See the selected answer [here](https://stackoverflow.com/questions/13542950/support-of-k-in-regex) for details. – Cary Swoveland Feb 24 '23 at 08:50
  • This solution works, although I had to group the expression after `\K` to make it work in Nim ( `COLOR1=\s*\K(.*)` ). The `\K` escape sequence is compatible with Nim because its regex library is based on PCRE. – Drackich Feb 24 '23 at 17:05
  • Drackish, having to save the string of interest to a capture group seems odd. Are you using the match or the contents of the capture group? If the latter `\K` is not needed. – Cary Swoveland Feb 24 '23 at 18:22
  • @CarySwoveland You are correct, I am trying to capture the content of the group. Removing `\K` works perfectly. – Drackich Feb 25 '23 at 00:20
1

Attempt to match the regular expression

(?m)^COLOR2=\s*(.+)

If there is a match capture group 1 will hold the desired string.

Demo

The expression has the following elements.

(?m)     # set the multiline flag
^        # match the beginning of a line
COLOR2=  # match literal
\s*      # match zero or more whitespace chars
(        # begin capture group 1
  .+     # match one or more chars other than line terminators 
)        # end capture group 1

The multiline flag m (in (?m)) causes ^ to match the start and end of a line. In some languages the multiline flag is set differently. In Ruby there is no need for that flag as ^ matches the beginning of a line (and \A matches the beginning of the string).

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100