Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


But are they as efficient as regexp? I personally prefer regexp combinators.


Optimizing them is a bit more work, but they can outperform hand-rolled C code:

http://www.serpentine.com/blog/2014/05/31/attoparsec/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: