dsnipe
Hello.
I need to create a RegExp from any string. I tried to use strings with special characters, e.g. "\d"
But when I try to compile this string, it escapes the symbols and shows something like ~r/\x7F/
The code to try:
regex_str = "^\d+,$"
Regex.compile(regex_str) # {:ok, ~r/^\x7F+,$/}
Thanks in advance for any suggestions!
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
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
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
That is because just using
"as the string delimiters turns on special characters like that.To turn it off there are a few ways, the usual one is via using the
~S(capital S) sigil, which turns interpolation and special characters off, thus:EDIT1: For note,
"basically is like having an implied~sbefore it. You can change the delimiter to a certain set if you want,",',/and a half dozen others are all valid, so~s/blah/ == "blah"is true.EDIT2: Also, the
rsigil both defines a string and passes it to regex compile all in the same step, so your original example could be done like:The inspection protocol for compiled regex’s just converts the compiles regex into the sigil form for easy copy/pasting into the shell, but that is not how it really is internally. The inspection protocol is for ease of ‘you’ reading it, not how it really is.
EDIT3: There are lots of sigils, you can even make your own, all documented at:
dsnipe
In my case I get a string from users input, and they expect to use a ‘normal’ regexp.
So I can’t use sigils, like
~S, because I can’t pass a variable to the macro.I have this string “^\d” as an input from users (or a record from a DB) and I need to convert it to regexp.
OvermindDL1
Well doing
regex_str = "^\d+,$"is most certainly not user input. ^.^It would be more like
regex_str = get_user_blah()or so. Strings are only escaped in ‘source code’, not from anywhere else. So if it is user input then it is already fine.The issue is that your binding content in
regex_strwas escaped, it was not theRegex.compile/1escaping it, it was the"parts above it.It is exactly the same in javascript, C, C++, ocaml, etc… etc… etc…, almost every language out.
michalmuskala
If you get
\dfrom an external source, it will be represented as"\\d"as a string, and this will work fine withRegex.compile.Elixir string
"\d"does not represent two characters, but a single one. A user entering text into, say, text field, does not use Elixir syntax, though, so when they type\d, this will result in a two-byte string, represented as"\\d"in Elixir. When you use a string in a test, you use Elixir syntax, so additional escaping is necessary there.dsnipe
Thanks for the explanation @michalmuskala.
Indeed, I used it in tests and faced this problem. Now, it’s clear for me.
michalmuskala
As a side note, be careful running user input as a regex. It’s very easy to create a pathologically expensive regex that will DOS your server.
OvermindDL1
You know I just got to thinking, is it possible to create a function to do a rough calculation of a ‘cost’ of a compiled regex? That way we could deny user inputted regex that goes above a certain ‘cost’? Sounds like a hard problem to catch all the detrimental cases… I wonder…
michalmuskala
That sounds to me an awfully lot like the halting problem.
OvermindDL1
To evaluate it then possibly, I was thinking more of just known detrimental patterns, like ones that can match unbounded and expensive loops and such.