You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently ran into a situation where I needed to use a regex with the Regex::CompileOptions::DOLLAR_ENDONLY option. However after using the Regex.new(String) overload to be able to set the option, the regex caused a PCRE2 stack size limit exceeded error. What ultimately ended up happening was the string being passed to Regex.new was like "[\|]", but was being interpreted as "[|]", changing the semantics of the regex.
The solution was to use %q() such that it was treated similar to a regex literal, which does not have this problem. So the root problem is that if you need to use regex modifiers that are not supported via a regex literal, you have to use the string version.
It would be nice to improve this use case, of which we have a few options that I could think of:
Add some additional modifiers to allow using regex literals
Add an additional Regex.new overload that accepts a Regex and passes it thru via #source. Would be kind of wasteful given it involves an extra Regex instance, but would allow passing extra options while still being able to use a literal for its ease of use and compile time checks.
Possibly just document that %q or similar should be used when using Regex.new(String)
TBD
The text was updated successfully, but these errors were encountered:
Discussion
I recently ran into a situation where I needed to use a regex with the
Regex::CompileOptions::DOLLAR_ENDONLY
option. However after using theRegex.new(String)
overload to be able to set the option, the regex caused a PCRE2 stack size limit exceeded error. What ultimately ended up happening was the string being passed toRegex.new
was like"[\|]"
, but was being interpreted as"[|]"
, changing the semantics of the regex.The solution was to use
%q()
such that it was treated similar to a regex literal, which does not have this problem. So the root problem is that if you need to use regex modifiers that are not supported via a regex literal, you have to use the string version.It would be nice to improve this use case, of which we have a few options that I could think of:
Regex.new
overload that accepts aRegex
and passes it thru via#source
. Would be kind of wasteful given it involves an extraRegex
instance, but would allow passing extra options while still being able to use a literal for its ease of use and compile time checks.%q
or similar should be used when usingRegex.new(String)
The text was updated successfully, but these errors were encountered: