r-lib / rex

Friendly regular expressions for R.
https://rex.r-lib.org
Other
334 stars 27 forks source link

Simplified capturing with named vectors. #22

Closed jimhester closed 10 years ago

jimhester commented 10 years ago

Unnamed capturing would remain the same with capture(), however we could also allow you to specify named captures by naming an argument to rex(). e.g.

rex(word=letters)

rex(two_words=list(letters, space, letters)

I am not sure the second form is much easier than

rex(capture(name = "two_words", letters, space, letters))

@kevinushey any thoughts, do you think it is worth having?

kevinushey commented 10 years ago

It sounds like it could be nice, but it could make handling other constructs a lot more messy, e.g.:

rex(maybe(two_words = list(letters, space, letters)))

It would imply more functions would need to understand named arguments -- this could still be feasible, but I am not sure.

I think I like this syntax -- it's more terse while still being clear -- but there may be edge cases in implementation that make it tricky.

jimhester commented 10 years ago

That is a good point. It also would get messy with the rex functions that also take arguments. I think we keep the syntax as it is for now, while it would be a little nicer I don't think it is worth dealing with all of the edge cases and increased implementation complexity. Thanks for your comments!