Friends, I've been wondering if it's possible to c...
# getting-started
p
Friends, I've been wondering if it's possible to compose regexes regexen regex things, e,g
Copy code
val words = Regex("""\w+""")
val digits = Regex("""\d+""")
combined into
\w+|\d+
via
words | digits
or similar. Any suggestions are welcome. Thanks! 🙂
v
You already wrote how to do it.
Besides that it is quite useless as
\w
already includes
\d
So
\w+|\d+
is identical to
\w+
e
I tend to find that the extension looks nicer,
Copy code
val words = """\w+""".toRegex()
also it is generally difficult to combine regexes. you could
Copy code
val both = "${words.pattern}|${digits.pattern)".toRegex()
but that will break back-references within each pattern, and doesn't handle differing regex options either
for example, it won't work to combine these two
Copy code
val re1 = """([a-z]+)""".toRegex(RegexOption.IGNORE_CASE)
val re2 = """(['"]).*\1""".toRegex(RegexOption.DOT_MATCHES_ALL)
m
I've wanted to play around with a regexbuilder before where you could do something akin to
Copy code
buildRegex{
    either{
        capture("""\w+""", name = "words")
        capture("""\d+""", name = "numbers")
        just("""\s+""")
    }
}
you could maybe make that?
gratitude thank you 1
v
Ah, you meant combining from the variables, sorry, I got that wrong. I thought you meant writing a regex that matches both. Then what @ephemient said. It is probably better to not try to do it unless you know for sure the patterns are combinable.
gratitude thank you 1
e
or build a parser combinator instead of regex
gratitude thank you 1
p
Thanks for the pointers, friends! I was just playing with my code for a talk and wondered if I could reuse/combine some regex I had (and thus have more problems 😁). A parser combinator sounds ideal. Cheers!
e
a sketch of a primitive parser combinator: https://pl.kotl.in/9TI8Rl6dD
K 1
gratitude thank you 1
🎉 1
p
@ephemient wow, this is brilliant, thank you! gratitude thank you