SE-0354 (Second Review): Regex Literals

One reasonable rule — not necessarily advocating, just musing — would be as follows:

  1. Remove comments
  2. For each line, remove all leading and trailing whitespace
  3. Remove newlines
  4. Any whitespace that remains is significant

For example:

    #/
        (
            hello        # morning
            |
            good night   # evening  (this and only this space character is preserved)
        )
        (
            ,\s+
            every
            (body|one)
        )?
   /#

…would be equivalent to:

/(hello|good night)(,\s+every(body|one))?/

Edit to add: We might want an additional rule that any space preceded by a backslash is not removed in step 2, so that this works:

#/
  hello\       # space after backslash is not removed, but subsequent spaces on this line are
  world
/#

Edit again: With the thread move, my reply to a reply to this post got out of order; note that I found @hamishknight’s counterargument compelling and prefer their proposed alternative.

7 Likes