What's the RegEx to make sure that delimiters are escaped?

Posted by Kuyenda on Stack Overflow See other posts from Stack Overflow or by Kuyenda
Published on 2012-12-02T05:02:01Z Indexed on 2012/12/02 5:03 UTC
Read the original article Hit count: 178

Filed under:

I'm looking for a regular expression that will check whether or not delimiters in a string are escaped with a backward slash.

The delimiters I am concerned about are comma (\,), colon (\:), semicolon (\;) and of course the backward slash itself has to be escaped (\).

For example, the string "test" should return a match because there are no delimiters in it, and no escaping is necessary. The string "te\;st" would return a match because the semicolon delimiter is escaped. "te;st" and "t\;s:t" would both fail because the both contain at least one delimiter that is not escaped.

I know that I need a conditional and a positive look behind, and this is what I have so far, but it is not giving me the expected answer.

^(?<delimiter>[:;,\\])?(?(delimiter)\(?<=(?:\\\\)*\\)k<delimiter>|.)$

Any suggestions on how I can make this work?

Thanks.

© Stack Overflow or respective owner

Related posts about regex