fireproofsocks

fireproofsocks

A \s inside a string represents a space, i.e. " ". In other words:

iex> " " === "\s"
true
iex> "this is a string" === "this\sis\sa\sstring"
true

However, inside a regular expression, the \s takes on a different meaning: it is shorthand for “whitespace character”, so it can match on tabs, newlines, and more. Here it can replace all of them in one swoop:

iex> str = "\tsome\nthing   with\f\nspaces\s\s"
"\tsome\nthing   with\f\nspaces  "
iex> Regex.replace(~r/\s+/, str, "-")
"-some-thing-with-spaces-"

Whereas if you just want to replace literal spaces, you have to explicitly use a space character and NOT \s:

iex> Regex.replace(~r/ +/, str, "-")
"\tsome\nthing-with\f\nspaces-"

By contrast, you can replace specific characters like tabs by referencing them literally:

iex> Regex.replace(~r/\t+/, str, "-")
"-some\nthing   with\f\nspaces  "

Can someone explain why this is the case? I just had this realization that \s is not a \s and I wanted to put the thought out to the community in a coherent post.

Related, I finally understand how the u unicode flag can affect the output. E.g. referencing a chart of whitespace characters, we can come up with a string that uses unicode whitespace characters:

iex> str = "Unicode\u00A0spaces\u2006"
"Unicode spaces "
iex> Regex.replace(~r/\s+/, str, "-")
"Unicode spaces "  # <-- spaces not matched!
iex> Regex.replace(~r/\s+/u, str, "-")
"Unicode-spaces-"  #< -- the u flag causes the spaces to be matched!

Showing Posts 1 to 2

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

How the \s is interpreted depends on the surrounding syntax, which all sigils have an opportunity to change. For example:

iex(3)> IO.puts "\s"
 
:ok
iex(4)> IO.puts ~S(\s)
\s
:ok
iex(5)> 

It isn’t so much about regular expressions as it is about sigils being allowed to interpret their inner contents.

Nicd

Nicd

You are mixing two different syntaxes. Strings and regular expressions are different things and have different escapes.

Strings/sigils in Elixir have these escapes: https://elixir-lang.org/getting-started/sigils.html#interpolation-and-escaping-in-string-sigils

Elixir’s regular expression library on the other hand is based on PCRE and it has the PCRE escapes, that are described here: re — OTP 29.0.2 (stdlib 8.0.1)

Both systems are different and any commonalities in their escapes are coincidental and a result of people gravitating towards the escapes that are already in common use when designing systems. So \s in a string is different from \s in a regex.

Note also that in strings/sigils, escapes are used to represent single, specific characters. In a regex escapes can be used to match certain ranges of characters (1..n). So they can’t be mapped to each other because they are for different things.

— 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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews