Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confused by Match type. #167

Open
Magicloud opened this issue Apr 10, 2019 · 5 comments

Comments

@Magicloud
Copy link

commented Apr 10, 2019

Apparently there is a matchArray contains all the captures. But I could not see it exposed anywhere.
So how should I get the sub matches (by parens)?

@Magicloud

This comment has been minimized.

Copy link
Author

commented Apr 10, 2019

Never mind. Forgot they are in RegexBase.

@Magicloud Magicloud closed this Apr 10, 2019

@bergmark

This comment has been minimized.

Copy link
Contributor

commented Jul 31, 2019

I got stuck on this as well, could someone give an example of how to extract captures?

I'm trying to do something like

_getMyTwoCaptures ("x,y" ?=~ [re|(.),(y)|]) == Just ("x", "y")

@cdornan cdornan reopened this Jul 31, 2019

@cdornan

This comment has been minimized.

Copy link
Contributor

commented Jul 31, 2019

@bergmark sorry, this is not clear and it should be fixed so I am leaving it open.

The main problem is probably that you need to use Text.RE.Replace to work with captures.

Hopefully these examples will makes things a bit clearer. (Note the ${a}(...) construction for naming captures.)

{-# LANGUAGE QuasiQuotes #-}

import Text.RE.Replace
import Text.RE.TDFA.String


-- lists all of the captures (Nothing => no match)
caps1 :: String -> Maybe (Capture String,[Capture String])
caps1 s = matchCaptures $ s ?=~ [re|(.),(y)|]

-- lists all of the captured strings
caps2 :: String -> Maybe [String]
caps2 = fmap (map capturedText . snd) . caps1

-- using capture numbers, extracts capture '1' and '2' ('0' is whole regex)
caps3 :: String -> Maybe (String,String)
caps3 s = (,) <$> mtch !$$? [cp|1|] <*> mtch !$$? [cp|2|]
  where
    mtch = s ?=~ [re|(.),(y)|]

-- using named captures, extracts capture 'a' and 'b'
caps4 :: String -> Maybe (String,String)
caps4 s = (,) <$> mtch !$$? [cp|a|] <*> mtch !$$? [cp|b|]
  where
    mtch = s ?=~ [re|${a}(.),${b}(y)|]

-- if you want partial code that assumes matches then you can do this
caps5 :: String -> (String,String)
caps5 s = (mtch !$$ [cp|a|], mtch !$$ [cp|b|])
  where
    mtch = s ?=~ [re|${a}(.),${b}(y)|]
@bergmark

This comment has been minimized.

Copy link
Contributor

commented Jul 31, 2019

Thanks! i got it working now.

I think I expected the Replace module to only deal with replacement, so I never looked in there.

@cdornan

This comment has been minimized.

Copy link
Contributor

commented Jul 31, 2019

Yes — I have had the sense that something was wring — this has helped bring it down not focus 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.