Find all regexp capture groups

I am looking for a Haskell function that returns capture groups for all matches of a given regular expression.

I watched Text.Regex but could not find anything.

Now I am using this workaround, which seems to work:

import Text.Regex findNext :: String -> Maybe (String, String, String, [String] ) -> [ [String] ] findNext pattern Nothing = [] findNext pattern (Just (_, _, rest, matches) ) = case matches of [] -> (findNext pattern res) _ -> [matches] ++ (findNext pattern res) where res = matchRegexAll (mkRegex pattern) rest findAll :: String -> String -> [ [String] ] findAll pattern str = findNext pattern (Just ("", "", str, [] ) ) 

Result:

 findAll "x(.)x(.)" "aaaxAxaaaxBxaaaxCx" [["A","a"],["B","a"]] 

Question:

  • Did I miss something in Text.Regex?
  • Is there a Haskell regex library that implements the findAll function?
+6
source share
1 answer

You can use the =~ operator from Text.Regex.Posix :

 Prelude> :mod + Text.Regex.Posix Prelude Text.Regex.Posix> "aaaxAxaaaxBxaaaxCx" =~ "x(.)x(.)" :: [[String]] [["xAxa","A","a"],["xBxa","B","a"]] 

Note the explicit type [[String]] . Try replacing it with Bool , Int , String and see what happens. All types that you can use in this context are listed here . Also see this tutorial .

+8
source

Source: https://habr.com/ru/post/892969/


All Articles