Erics
Erics

Reputation: 841

Regex to also handle escaped delimiters?

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,

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:

Additionally, the backslash itself could be escaped, like this

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

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions