-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 a function to escape strings for use in regular expressions #29643
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -449,3 +449,27 @@ function hash(r::Regex, h::UInt) | |
h = hash(r.compile_options, h) | ||
h = hash(r.match_options, h) | ||
end | ||
|
||
## escaping ## | ||
""" | ||
regex_escape(s::AbstractString) | ||
|
||
Sanitize a string to make it safe for use in regular expression pattern construction. Any | ||
regular expression metacharacters are escaped along with whitespace. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> regex_escape("Bang!") | ||
"Bang\\!" | ||
|
||
julia> regex_escape(" ( [ { . ? *") | ||
"\\ \\ \\(\\ \\[\\ \\{\\ \\.\\ \\?\\ \\*" | ||
|
||
julia> regex_escape("/^[a-z0-9_-]{3,16}\$/") | ||
"/\\^\\[a\\-z0\\-9_\\-\\]\\{3,16\\}\\\$/" | ||
``` | ||
""" | ||
function regex_escape(s::AbstractString) | ||
res = replace(s, r"([()[\]{}?*+\-|^\$\\.&~#\s=!<>|:])" => s"\\\1") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe my original was based on PHP, but adding some precautionary characters from Python, and escaping whitespace. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (But I haven’t looked at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (My comments end with this version, BTW.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like |
||
replace(res, "\0" => "\\0") | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -936,6 +936,17 @@ ERROR: syntax: invalid escape sequence | |
Triple-quoted regex strings, of the form `r"""..."""`, are also supported (and may be convenient | ||
for regular expressions containing quotation marks or newlines). | ||
|
||
The `regex_escape` function allows you to escape a string for use in constructing a regular | ||
expression. All whitespace and PCRE metacharacters are escaped. | ||
|
||
```julia-repl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this be a doctest? |
||
julia> regex_escape("Bang!") | ||
"Bang\\!" | ||
|
||
julia> regex_escape(" ( [ { . ? *") | ||
"\\ \\ \\(\\ \\[\\ \\{\\ \\.\\ \\?\\ \\*" | ||
``` | ||
|
||
## [Byte Array Literals](@id man-byte-array-literals) | ||
|
||
Another useful non-standard string literal is the byte-array string literal: `b"..."`. This | ||
|
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.
Probably better call this
escape_regex
, for consistency withescape_string
?