4 ms·
Parsing combinators are the API you want. regexReplaced n = do char 'T' spaces x <- concat (replicateM n alphaNum) y <- concat
by lalaithion 9y ago
Parsing combinators are the API you want.
regexReplaced n = do
char 'T'
spaces
x <- concat (replicateM n alphaNum)
y <- concat (many alphaNum)
spaces
char '='
return (x ++ y)
The above is a Haskell function that does the parsing required above, returning the alphanumeric characters if the parse succeeds and returning an error if it does not. You may not speak Haskell, but this is probably still more readable than (n) => {new RegExp(`T\s(\w{${n}}\w)\s*=`)}, which is the Javascript function that does a similar thing.
- xfer 9y agoBut are they as efficient as regexp? I personally prefer regexp combinators.
- lalaithion 9y agoOptimizing them is a bit more work, but they can outperform hand-rolled C code: http://www.serpentine.com/blog/2014/05/31/attoparsec/ http://www.serpentine.com/blog/2014/05/31/attoparsec/