caleb-bb

caleb-bb

Regex.scan and repeated words

So I define my regex like so:

{:ok, reg} = Regex.compile("^#{word} | #{word}$|^#{word}$| #{word} ")

With word = "go"

Then I run

Regex.scan(reg, "go go gordon go") |> List.flatten

And I’m expecting to get ["go", " go ", " go"] but instead I get ["go ", " go"].

What am I doing wrong? Does it have to do with how the regex is organized? I’m doing it like this to avoid matching the go in gordon.

Marked As Solved

lucaong

lucaong

You have four alternatives in your regexp:

  1. The word at the beginning of the string, then a space
  2. A space, then the word at the end of the string
  3. The word at the beginning of the string, then the end of the string
  4. A space, the word, then another space

When you scan go go gordon go the following things happen: rule 1 matches go , the second go doesn’t match because it’s neither at the beginning, nor at the end, nor it has a space before AND after (the space before was already consumed by the first match), gordon as expected doesn’t match, finally the last go matches rule 2.

In fact, if you had a second space after the first go you would get the match you expect.

One possible way to match all the “go” that are not part of another word is ~r/\bgo\b/ (the \b represents a word boundary). It is not equivalent to your expectation, because it does not return spaces in the matches, but maybe it is what you ultimately want?

word = "go"
{:ok, reg} = Regex.compile("\\b#{word}\\b")
Regex.scan(reg, "go go gordon go") |> List.flatten()
# => ["go", "go", "go"]

Also Liked

trisolaran

trisolaran

Yep, @lucaong explained what’s going on. To solve the problem, I was about to suggest using negative lookahead + lookbehind, like this:

~r/(?<![^\s])go(?![^\s])/

This works but @lucaong’s solution using \b is way more elegant

caleb-bb

caleb-bb

Marking this one as solution because it explains the “why” behind the issue very well. You even saw through my clumsy coding and understood my underlying intention. Thank you! And thanks to @trisolaran as well.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement