parser-regex
is a regex library where regexes are composed together in the style of parser-combinator libraries.
import Regex.Text (REText)
import qualified Regex.Text as R
import qualified Data.CharSet as CS
reKeyValues :: REText [(Text, Text)]
reKeyValues = R.char '{' *> (kv `R.sepBy` R.char ',') <* R.char '}'
where
str = R.someTextOf CS.asciiLower
kv = (,) <$> str <* R.text ":" <*> str
>>> import qualified Regex.Text as R
>>> R.reParse reKeyValues "{foo:bar,baz:qux}"
Just [("foo","bar"),("baz","qux")]
This makes parsing a lot more fluent compared to matching a regex pattern, extracting the submatches, potentially parsing those submatches, and putting them all in the result.
At the same time, it stays in the relatively simple world of regular expressions and avoids the complexity of more powerful parsing libraries.
You can find more details and examples in the README.
If you have used the regex-applicative library before, things should sound familiar. I thought it was a great idea and tinkered with different internals, which ultimately led to this library. parser-regex
offers better performance, Text
support, and more handy definitions.
Feedback is welcome!