Reputation: 1789
I'm having an issue understanding how I can capture everything after an optional prefix, unless, that prefix is displayed again. It would be nice to have the [FFFFFF] tag get matched a start over as group 1 vs being an optional additional group.
I'm new-ish to regex and I love the regex101.com site but this just appears to be above my level of understanding.
Expression:
(\[[0-9a-fA-F]{6}\])?(.*)
Sample Text
[00B800][!] Green - [004DB8] Blue does not match!!
[FFFFff][!] White
[e60000][!] Red
123456
Test string, another test abcdef
This is the result I'm attempting to get.
Link
https://regex101.com/r/z3CbHm/2/
Upvotes: 0
Views: 776
Reputation: 163457
You could use 2 capturing group, making the first group optional.
Then for the second group, match either until you see the next occurrence of the pattern \[[0-9a-fA-F]{6}\]
or the end of the string.
(\[[0-9a-fA-F]{6}\])?(.+?)(?=\[[0-9a-fA-F]{6}\]|$)
If you want to omit possible leading whitespace chars for the second group:
(\[[0-9a-fA-F]{6}\])?(?:[^\S\r\n]*(.+?))(?=\[[0-9a-fA-F]{6}\]|$)
That will match
(\[[0-9a-fA-F]{6}\])?
Match the pattern in optional group 1(?:
Non capture group
[^\S\r\n]*
Match 0+ occurrences of a whitespace char except a newline(.+?)
Capture group 2, match 1+ occurrences of any char except a newline)
Close group(?=
Positive lookhead, assert what is on the right is
\[[0-9a-fA-F]{6}\]
Match the pattern|
Or$
End of string)
Close lookaheadUpvotes: 1