Has it ever been considered to allow concatenation...
# stdlib
d
Has it ever been considered to allow concatenation of `Regex`es that contain different options? A decent number of times, I've had a case where I want to build a regex that is composed mostly of constants, but with a lot of characters that would require escaping. For a very simplified example, it would be very handy if, instead of:
Copy code
Regex("""java\.lang\.ArrayIndexOutOfBoundsException: Index \d+ out of bounds for length \d+""")
I could write:
Copy code
Regex("java.lang.ArrayIndexOutOfBoundsException: ", RegexOption.LITERAL) + Regex("""Index \d+ out of bounds for length \d+""")
r
in this case you could also use:
Copy code
\Qjava.lang.ArrayIndexOutOfBoundsException:\E Index \d+ out of bounds for length \d+
(everything between
\Q
and
\E
is escaped) or alternatively you could use the
Regex.escape
function, depending on your preference, like:
Copy code
Regex("""${Regex.escape("java.lang.ArrayIndexOutOfBoundsException:")} Index \d+ out of bounds for length \d+""")
btw, also note that the index in an out of bounds exception could also be negative, so you'd need to account for e.g. an index of -1, unless you're specifically matching for positive indices :)
d
Good to know, thanks! I had forgotten to check the
Companion
page for
Regex
, and only looked at the main page.
e
regexes are not actually regular expressions, they may include named and numbered groups and back references. that makes combining them a challenge