JuliaLang/julia — Issue #340: simple way to cut down the number of r*_str macros
Author: StefanKarpinski
Created: 2012-01-13 21:00:42+00:00
State: open
Labels: enhancements, features, speculative

There are an absurd number of `r*_str` macros for creating regex objects at compile time via non-standard string literals. These exist to support some of the options one can apply to a regex when creating it. However, it's really annoying and silly to create a macros for each possible combination of regex flags. Many valid but less common regex flags are omitted because of the combinatorial explosion that would ensue if they were included.

I propose that instead, non-standard string literals have an optional suffix as well, which, if present, is passed to the macro as a second string argument. Then we could have this definition for regex literals:

```
const REGEX_FLAGS = {
'i' => PCRE_CASELESS,
'm' => PCRE_MULTILINE,
's' => PCRE_DOTALL,
'x' => PCRE_EXTENDED,
}

macro r_str(pattern, flags)
options = int32(0)
for x in flags
options |= PCRE_OPTIONS[x]
end
Regex(pattern, options)
end
```

This single definition would make the following regex literals with flags work:

```
r"^[a-z]+$"i # => case insensitive
r"/\*.*?\*/"sm # => multiline & dotall
r" ^ # start
([a-z]+) # protocol
:// # colon-double-slash
([a-z\.]+) # domain
... # etc.
$ # end
"xi # => case insensitive and extended syntax
```