Skip to content

Commit 31521dd

Browse files
torfjeldedevmotion
andauthored
Escape regex in namesingroup properly (#398)
* escape index properly * bump patch version * Update src/chains.jl Co-authored-by: David Widmann <devmotion@users.noreply.github.com> --------- Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
1 parent 711a298 commit 31521dd

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
33
keywords = ["markov chain monte carlo", "probablistic programming"]
44
license = "MIT"
55
desc = "Chain types and utility functions for MCMC simulations."
6-
version = "5.7.0"
6+
version = "5.7.1"
77

88
[deps]
99
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/chains.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ julia> namesingroup(chn, :A)
137137
2-element Vector{Symbol}:
138138
Symbol("A[1]")
139139
Symbol("A[2]")
140+
141+
julia> # Also works for specific elements.
142+
namesingroup(chn, Symbol("A[1]"))
143+
1-element Vector{Symbol}:
144+
Symbol("A[1]")
145+
140146
```
141147
```jldoctest
142148
julia> chn = Chains(rand(100, 3, 2), ["A.1", "A.2", "B"]);
@@ -155,7 +161,7 @@ function namesingroup(chains::Chains, sym::Symbol; index_type::Symbol=:bracket)
155161
idx_str = index_type == :bracket ? "[" : "."
156162
# Start by looking up the symbols in the list of parameter names.
157163
names_of_params = names(chains)
158-
regex = Regex("^$sym\$|^$sym\\$idx_str")
164+
regex = Regex("^\\Q$sym\\E\$|^\\Q$sym$idx_str\\E")
159165
indices = findall(x -> match(regex, string(x)) !== nothing, names(chains))
160166
return names_of_params[indices]
161167
end

0 commit comments

Comments
 (0)