dsnipe

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!

Showing Posts 1 to 9

OvermindDL1

OvermindDL1

That is because just using " as the string delimiters turns on special characters like that. :slight_smile:

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:

iex> "\d"
"\d"
iex> ~s"\d"
"\d"
iex> ~S"\d"
"\\d"

:slight_smile:

EDIT1: For note, " basically is like having an implied ~s before 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. :slight_smile:

EDIT2: Also, the r sigil both defines a string and passes it to regex compile all in the same step, so your original example could be done like:

iex> regex_str = "^\\d+,$"
"^\\d+,$"
iex> Regex.compile(regex_str)
{:ok, ~r/^\d+,$/}
iex> ~r"\\d+,$"
~r/\\d+,$/
iex> ~r/\\d+,$/
~r/\\d+,$/
iex> ~r{\\d+,$}
~r/\\d+,$/

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. :slight_smile:

EDIT3: There are lots of sigils, you can even make your own, all documented at:

dsnipe

dsnipe OP

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

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. :slight_smile:

The issue is that your binding content in regex_str was escaped, it was not theRegex.compile/1 escaping it, it was the " parts above it.

It is exactly the same in javascript, C, C++, ocaml, etc… etc… etc…, almost every language out. :slight_smile:

michalmuskala

michalmuskala

If you get \d from an external source, it will be represented as "\\d" as a string, and this will work fine with Regex.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

dsnipe OP

Thanks for the explanation @michalmuskala.
Indeed, I used it in tests and faced this problem. Now, it’s clear for me.

michalmuskala

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

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

michalmuskala

That sounds to me an awfully lot like the halting problem.

OvermindDL1

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews