Regex to match strings in JLex

307 views Asked by At

I'm trying to fix this regex, it's meant to match any string of characters except unescaped quotation marks and unescaped newline characters:

([^"]|\\"|[^\n]|\\n)*

Would anyone mind helping out?

For example I would want to match:

The cow jumped over the \\"moon

but not:

The cow jumped over the "moon

Same for newlines

1

There are 1 answers

1
anubhava On

You may use this regex:

^[^"\n\\]*(\\.[^"\n\\]*)*$

RegEx Demo

RegEx Details:

  • ^: Start
  • [^"\n\\]*: Match 0 or more of any char except newline. backslash and "
  • (: Start 2nd group
    • \\: Match literal backslash
    • .: Match any character following a \
    • [^"\n\\]*: Match 0 or more of any char except newline. backslash and "
  • )*: End 2nd group. Match 0 or more of this group
  • $: End