jxxcarlson

jxxcarlson

Regex weirdness -- Aargh!

I have a Regex that works correctly in tests but not when my run my app. No idea what is going on. Here is the regex expression: Regex.scan(~r/^\[([a-z].*)\].--.(.*).--/msU, text) .
Here is the string I apply it to:

[quote]
--
foo, bar
baz
--

fdfd

The correct scan result is

# [quote]
# --
# foo, bar
# baz
# --
# fdfd

["[quote]\n--\nfoo, bar\nbaz\n--",
"quote",
 "foo, bar\nbaz"]

This is what I get using the regex tester at elixre.uk and also when I run tests with mix test ... However, when I use the above regex expression in my app, the result is an empty list – the regex recognizes nothing. What to do?

Marked As Solved

NobbZ

NobbZ

Your regular expression fails for me if I put windows line endings in the string ("\r\n") instead of Linux ones ("\n"). The option

This is, because . does match only \r then, after that the regex demands to get a - but there is \n, so it fails.

So you have 3 options:

  1. Demand the client to deliver only Linux-Endings, but depending on the tech knowledge of the client that may be impossible
  2. Normalize the input before feeding to the RE-scan, which should be the easiest one
  3. replace the dots that shall match line endings by (?:\r|\n|\r\n), but the output needs to be cleaned up as well.

Also Liked

jxxcarlson

jxxcarlson

Thank you! I choose your suggested option (2). Simple and works like a charm.

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

We're in Beta

About us Mission Statement