-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add named group support #6
Comments
Named groups is something that is missing in at least JavaScript, so this could potentially be an issue with cross-language support.
|
How about something like:
|
I was thinking more of like the Python implementation:
The real problem is, that some languages (e.g. JavaScript) do not natively support named groups for regex, like python does. The following would be implemented in vanilla python regex like:
Which allows us to ask values for So getting this to function in languages that don't support named groups would be awesome. |
Not bad, but I'm not a fan of the dangling, JavaScript-esque parentheses. |
Also adding group members as arguments would kind of be against how the rest of the library works. How about automatically wrapping the preceding "ungrouped" part automatically with Like:
that would compile to:
|
I think it's a little clearer to label the start/end of a group instead of just the end. |
Hmm.. But then there's a human error possibility, especially with large expressions. If a dev forgets to start/end one group, that can be hard to debug. If there's just a general
that would create an unnecessary group around the |
How about |
But consider if you'd have a big expression with lots of things going on. You start modifying the code and somewhere between the copy-pastes you lose a I really think using just the single |
I don't think we need to worry about cases where people mess up their code, there's not much we can do about that. That said, if there's an I just think it should be clear that a group has a well defined start and end. Also a single |
I would also vote for an approach with 2 methods. It makes it more obvious.
In this case you specify the name only when you open it. To avoid issues in situations like the one below we could add the group wrapper to the expression only when you call the
For debugging maybe we could add a helper method like |
Given the variety of languages that implement VerbalExpressions I just want to throw in my opinion that I hope we can avoid functions that throw, as some languages don't have a concept of exceptions or try/catch and so the failure case would have to be lifted, adding friction to the actual use of the API |
I'd do that by default just to cut down on errors like @jehna mentioned. I also think it's better to return lists of dicts (or namedtuples?) with results instead of |
Or we could just omit the whole |
Also a bit related: VerbalExpressions/purescript-verbal-expressions#2 |
Thanks for the reference. I have implemented this now. It allows to write something like this: pattern = do
find "start"
some whitespace
possiblyV do
find "middle"
some whitespace
find "end" I realize it probably doesn't help this discussion here too much, but its a nice example where a monadic interface has some advantages over the JS-style method-chaining interface, because we can also bind to intermediate names: pattern = do
firstWord <- capture word
whitespace
capture word
whitespace
findAgain firstWord This pattern matches "foo bar foo" but not "foo bar baz". The expression This also allows us to "simulate" named groups for purposes of replacing. For more details, see the Replacing with named groups example. |
As implemented in Python repo's pull request by @andrii1812
VerbalExpressions/PythonVerbalExpressions#16
The text was updated successfully, but these errors were encountered: