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

Document matching end of line

Open alexanderkjeldaas opened this issue 9 years ago • 7 comments

I couldn't find an example on how I match [0-9]+[.][0-9][0-9]$ - that is, match something at the end of a line.

alexanderkjeldaas avatar Feb 10 '16 21:02 alexanderkjeldaas

Use the function to match the whole line, and include the equivalent of .* at the beginning of the pattern.

UnkindPartition avatar Feb 11 '16 10:02 UnkindPartition

Not sure if this is a good idea, as it isn't very composable.

Say I want to match "(foo($| )|bar)" then now I have to rewrite it to "(foo ?|bar.*)" that last expression is no longer composable as the first one was.

alexanderkjeldaas avatar Feb 11 '16 14:02 alexanderkjeldaas

Well, it is the way at the moment.

I don't have the resources to investigate whether and how this can be implemented, but you can look into it yourself if you wish.

UnkindPartition avatar Feb 11 '16 16:02 UnkindPartition

I wanted to remove 'index.html' from string. I found out that the following code was working:

stripIndex :: String -> String
stripIndex = takeWhile (/= '\0') .
             replace ((:) <$> sym '/'
                          <*  string "index.html"
                          <*> (concat <$> many query)
                          <*  sym '\0') .
             (++ "\0")
  where
    isDelim = (`elem` ['/', '#', '?'])
    query   = (:) <$> (sym '#' <|> sym '?') <*> many (psym $ not . isDelim)

By generalizing this, can we solve this issue?

demokritos avatar Feb 18 '20 07:02 demokritos

@demokritos why do this instead of https://github.com/feuerbach/regex-applicative/issues/30#issuecomment-182798229?

UnkindPartition avatar Feb 18 '20 10:02 UnkindPartition

Did you mean this?

stripIndex :: String -> String
stripIndex str = fromMaybe str $
                 match ((\a b c -> a ++ b : c) <$> many anySym
                                               <*> sym '/'
                                               <*  string "index.html"
                                               <*> (concat <$> many query)) str
  where
    query   = (:) <$> (sym '#' <|> sym '?') <*> many (psym (/= '/'))

I didn't know that match can replace.

I wonder whether the syntax below is possible by introducing special boundary indicators.

stripIndex = replace ((:) <$> sym '/'
                          <*  string "index.html"
                          <*> (concat <$> many query)
                          <*  EndOfLine)

Thanks.

demokritos avatar Feb 18 '20 11:02 demokritos

Yeah, I think it could be possible.

My answer is the same as 4 years ago (wow, time flies!): I'd consider a carefully motivated and implemented pull request, but I don't have the resources to look into this myself.

UnkindPartition avatar Feb 18 '20 19:02 UnkindPartition