0

I am attempting to write some regex that detects a valid string (with single quotes). I specifically mean that it is looking for valid string literals, as in don't match this text 'but do match this text'.

This is being done in a TextMate grammar (.tmLanguage.json file) which uses Oniguruma regex (used by Ruby and some versions of PHP). Because it is a TextMate grammar, it also means I can use begin and end keys to have regex that matches the beginning and end of what I want to match, and everything in between is automatically matched. I want to ensure that multiline stings will work, but only if they have a backslash before the new line. Because of how these grammar files work, if I want to have multiline matches, I must use begin and end.

Overall, here are some examples of strings that should/should not match:

'match this'
not this 'match this' not this
'match this\
and this'
'dont match this
and dont match this'
'match this \'and this too'
"dont match this"
'dont match this"

I tried using lookbehinds and lookaheads, but it was difficult to make it work with the begin/end structure. I was able to write a regex string as you would with normal regex ("match": "'(\\'|\\\n|[^'\n])*'"), but no matter what I do it cannot work for multiline matches in a TextMate grammar file. When searching online, I was unable to get anything about matching actual literal strings, I could only find how to tell if my regex matches a string (as in just a piece of text), and nothing about begin/end regex.

To sum up, how would I create regex that matches single quote strings, which can be multiline if they have a backslash before the newline, using begin/end regex expressions?

Finn E
  • 309
  • 2
  • 12

0 Answers0