cohen
I have some code that finds all instances of the characters , %, and _, inserting a backslash in front of them to escape them in the resulting SQL string. I’m a little bit confused about the amount of \ characters I need to use to do this. I would think that String.replace(string, ~r/([\\%_])/, "\\\\1") would do it, since I put in "\\" for a single backslash, then "\\1" for the backslash-one syntax to get my first capture. However, this results in substituting the characters backslash and 1, e.g., a_b → a\1b (on IO.puts).
It seems like this because "\\\\1" is the intended syntax for substituting an actual, literal backslash and "\\\\\\1" does the trick, but to be honest I’'m confused about how the escaping is actually working in this case.
Does anyone have any insights?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance











First 4 of 4 Posts
NobbZ
In the replacement language
\has a special meaning. So if you want it literally, you need to escape it.Your string
\\\\1is seen by the replacement language as\\1, which will result in the replace of\1(as printed) or\\1(as inspected).To actually get a single backslash followed by the content of the capture, you need 3 backslashes followed by a one in the replacement language, which in a string literall have to be doubled, such that you end up with 6 of them.
When I do write replacments, I usually use
~Sto avoid the duplication, then I can do~S"\\\1".cohen
Thanks, @NobbZ! I think I see what you mean by “replacement language”. This seems to be a special case when a regex is passed as the
patterntoreplace/4, as illustrated below:Is this behavior documented anywhere? I know the docs mention using “\1”, etc. to do capture substitution, and that implies that
\is being treated specially, but I’m still surprised that even without capture replacement regexpatterns causereplacements to behave differently.Edit: For a simple regexes, one can avoid the replacement language by either 1) supplying a list of strings as the
patternargument, or a function as thereplacementargument.cohen
Ah, and when
replacementis a function inRegex.replace/4, it behaves a little differently thanString.replace/4. For Regex, the function gets n + 1 arguments where n is the number of captures in the regex. The first argument is the whole match and next n correspond to each match, whereasString.replace/4’s replacement function only ever takes a single argument (the whole match).gcb
(i’ve spent too much time looking at sources on a friday to understand this exactly thanks to How to prevent LIKE-injections, so i’m going to post my notes on this 4yr old thread :^) sorry
regex/string.replace methods have their own extra parser, after the string one.
With strings there are no surprises. Two slashes are one slash and anything special gets converted if they have their own slash.
for regex and it’s replacements, all the slashes are parsed from left, matching pairs for the string parsing… and then the same happens again for the special chars in the those methods, including slashes/double slashes again. …if i got these right
I do not think all languages do that (e.g. elixir, php and ruby). Usually you just have to deal with one level to achieve everything (or be limited by it i guess), unless passing to another eval step that will re-parse the string explicitly. This feels like a extra half eval so to say. Or maybe other languages provide special cases for strings when parsed in a regex.
for example, in perl/python/PCRE you can replace with
"\\\1"and it’s fine.(with PCRE2 they changed
\1to$1so its harder to compare).seeing with ~S helps as it removes the first string parser from the picture and then things behave like most other languages. it is easy to see when you are escaping the slash that would trigger the special case you wanted.
"\\\\0" or ~S(\\0)here it is easy to see the first two slashes just became one slash and the zero never gets “activated” because the first slash (one the “second” parser) is escaped and not activating anything."\\\\\\0" or ~S(\\\0)you get the two slashes turned into one slash, and then the zero with it’s activation slash…It kinda makes sense when the pairs matches, but when they “leak” to the side it feels stranger.
in the end, i think i should have been using
~Sinstead of"everywhere…!…
PS: if thought that was bad, javascript “wins” by parsing both
$1and\$1the same. PHP8 does the same with PCRE1 compatibility on.