Reputation: 841
Say I have an input text cells of data delimited by colons, and I want to split that into matches based on runs of one or more colons, ignoring any initial leading colons.
Thus,
data_A:data_B:data_C:
is split into data_A:
, data_B:
, data_C:
:data_A:data_B:data_C:
is split into data_A:
, data_B:
, data_C:
::data_A:data_B:::data_C:
is split into data_A:
, data_B:::
, data_C:
A regex of /[^:].*?(?::+|$)/g
appears to work. https://regex101.com/r/JuMZ65/1
However, I need to make this work where the colon character itself might be in a data cell, in which case it would be written as \:
.
The above regex fails:
data_A::data_\:B:
is split into data_A::
, data_\:
, B:
data_A::
, data_\:B
Additionally, the backslash itself could be escaped, like this
data_A::data_\:B:data_\\:C
should get split into data_A::
, data_\:B
, data_\\:
, C
What regex will work, handling \:
and \\:
?
(My thinking process is not much more sophisticated than just throwing mud at the wall, so I'll spare you the mess of dozens of slight variations, half of which simply die on the table.)
Upvotes: 1
Views: 59
Reputation: 627600
You can use
/(?:[^:\\]|\\.)+(?::+|$)/g
// If the match can have no trailing colons not at the end of string:
/(?:[^:\\]|\\.)+:*/g
See the regex demo. Details:
(?:[^:\\]|\\.)+
- one or more occurrences of
[^:\\]
- any char other than :
and \
|
- or\\.
- an escaped char other than a line break char(?::+|$)
- either one or more colons or end of string.Upvotes: 1