2

I'm trying to write a regex that will match Oracle q-quotes for a PL/SQL lexer in code-prettify.js. For example,

q'[Here's Johnny]'

This should be matched the same as

'Here''s Johnny'

(that is, so that SQL will treat it all as one text string. The advantage of q-quotes over the conventional two-single-quotes is you don't have to go through your text string doubling up all of your single quotes.)

The quote delimiter can be any of [, {, <, or (, but I think if I can get it working with one bracket type then I can repeat the variations as ORs, like

/^(?:pattern1|pattern2|pattern3))/

Ultimately I want a single regex that will match an ordinary single-quoted string or a q-quote with any of the bracket types.

2
  • It should match to 'Here''s Johnny' ? That's a double ' or a single " ?
    – Onilol
    Commented Aug 27, 2015 at 17:46
  • @Onilol - either, but I meant two single quotes. That's the standard way of quoting a quote in SQL. Commented Aug 28, 2015 at 8:18

1 Answer 1

3

For your lexer you would like the text of q'[Here's Johnny]' and 'Here''s Johnny' to be matched. Assuming that you want the match to include all characters comprising the string token, including quotes, brackets, etc, this regular expression should work:

(?:q'\[.*?\](?=')'|q'<.*?>(?=')'|q'\(.*?\)(?=')'|q'{.*?}(?=')'|(?!q)'(?:[^']|'')*')

The two relevant pieces are:

  1. q'\[.*?\](?=')' is the basis for q-quoted strings, and the rest of the appropriate brackets have their own statements, and
  2. '(?:[^']|'')*', matches single-quoted strings.

You can see matching examples here.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.