regex-applicative icon indicating copy to clipboard operation
regex-applicative copied to clipboard

Support maximum repetition count

Open josephcsible opened this issue 6 years ago • 6 comments

Consider the regex \d{,6}. I'd like this library to contain a function replicateAlt that lets me implement this regex as replicateAlt 6 digit. One possible implementation would be a duplicate of replicateM, but with <|> pure [] on the end of the recursive case.

If you like this idea, I can PR it, but I'm not sure which module you want it in.

josephcsible avatar Oct 31 '19 01:10 josephcsible

Does this do what you want?

I'd accept a PR explaining (in the haddocks) how to do this for others who may be looking for the same thing.

UnkindPartition avatar Oct 31 '19 17:10 UnkindPartition

Yes, that's what I need for now. Although it looks like they only have a greedy one, so if I instead needed the non-greedy \d{,6}?, I'd be right back to needing to roll my own.

josephcsible avatar Oct 31 '19 17:10 josephcsible

Ah, good point. In that case it may be worth defining our own function.

UnkindPartition avatar Oct 31 '19 18:10 UnkindPartition

I dropped in intending to ask this in a separate issue, but this seems like a good spot:

What is the best way to quantify numerically? I know you can cobble something together out of what's already there. For instance, to match exactly n digits I can do

ghci> dig :: RE Char String; dig = pure <$> psym isDigit
ghci> <string> =~ (foldr1 (<>) $ replicate n dig)

But is this more or less it? Or is there some count-style combinator already built in, that I'm missing in the docs?

stuart-little avatar Apr 27 '21 21:04 stuart-little

Have you seen the count combinator linked above?

UnkindPartition avatar Apr 28 '21 08:04 UnkindPartition

Have you seen the count combinator linked above?

My apologies: I did see it, but did not understand the context..

I hadn't realized it applies to all instances of alternative. I thought it was more like the count I linked to, which was specific to ReadP.

Thank you!

stuart-little avatar Apr 28 '21 11:04 stuart-little