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

Document retrieving capture group names from Regex or RegexMatch #40347

Closed
anandijain opened this issue Apr 4, 2021 · 3 comments · Fixed by #40486
Closed

Document retrieving capture group names from Regex or RegexMatch #40347

anandijain opened this issue Apr 4, 2021 · 3 comments · Fixed by #40486

Comments

@anandijain
Copy link
Contributor

The Regex docs provide indexing with Symbol, where the symbol is the name of the capture group.

What is lacking is a way to retrieve the capture group names from either a Match or just the Regex itself (from the docs).

s = "aaa bbb ccc"
r = r"(?<a>.*) (?<b>.*) (?<c>.*)"
m = match(r, s) # I don't think there is a way to retrieve the symbols [:a, :b, :c] from this or `r`

One has to know about Base.PCRE.capture_names(m.regex.regex).

The context in which this arose was wanting to easily construct a DataFrame from a Vector{RegexMatch} where the capture group names become the column names. Thanks to @pdeffebach for finding the capture_names function for me.

This should be a simple PR to add a line to https://docs.julialang.org/en/v1/manual/strings/#Regular-Expressions mentioning it.

I may get to it, I may not xd

@BenjaminGalliot
Copy link
Contributor

BenjaminGalliot commented Apr 5, 2021

Related to #37299?

From the master branch, we now have a keys function.

s = "aaa bbb ccc"
r = r"(?<a>.*) (?<b>.*) (?<c>.*)"
m = match(r, s)
d = Dict(keys(m) .=> m)
# Dict{String, SubString{String}} with 3 entries:  "c" => "ccc"  "b" => "bbb"  "a" => "aaa"

@anandijain
Copy link
Contributor Author

beautiful, indeed this is a duplicate. i will leave it open for documenting keys, as that PR just updated NEWS.md

@regier21
Copy link
Contributor

regier21 commented Apr 5, 2021

I was looking into doing this and I noticed that none of the RegexMatch functions (or RegexMatch itself) are documented. Should I go ahead and add documentation for all of these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants