-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Regex: support duplicated named captures #5061
Regex: support duplicated named captures #5061
Conversation
a8a3f63
to
d7dcfee
Compare
d7dcfee
to
84725bd
Compare
Current crystal compiler cannot compile regexp containing duplicated named captures, so CI is failed. Should I replace regexp literal to |
I should fix this document also. |
#5061 (comment) I want some opinion to this. /cc @asterite @mverzilli @RX14 |
Yeah, using |
@RX14 fixed this. |
src/regex/match_data.cr
Outdated
private def named_capture_number(group_name) | ||
first = Pointer(UInt8).null | ||
last = Pointer(UInt8).null | ||
name_entry_size = LibPCRE.get_stringtable_entries(@code, group_name, pointerof(first), pointerof(last)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can out first, out last
be used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a tiny request to use out
if possible, otherwise this is good, thank you!
src/regex/match_data.cr
Outdated
@@ -157,10 +157,24 @@ class Regex | |||
# "Crystal".match(/r(?<ok>ys)/).not_nil!["ok"]? # => "ys" | |||
# "Crystal".match(/r(?<ok>ys)/).not_nil!["ng"]? # => nil | |||
# ``` | |||
# | |||
# When there are capture groups having same name, it returns the lastest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be "last". There is another place where this should be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes...
Currently we cannot use duplicated named captures by default:
But we can use
(?J)
for this:Unfortunately,
Regex::MatchData
does not support duplicated named captures, so this code is broken:This PR fixes
Regex::MatchData
to support duplicated named captures, andRegexp
accepts duplicated named captures by default. Duplicated named captures are useful when writing complex regexp. Thank you.