adrian

adrian

Regex and unicode returning wrong indexes

Hello,

I need to find the position of a match using Regex.run on a text with unicode characters.

This is an example:

iex> Regex.run(~r/x/u, "áéíóúx", return: :index)
[{10, 1}]

When te expected output should be: [{5, 1}]

Shouldn’t the unicode modificator take care of this? how could I get the right result?

Thanks!

First Post!

idi527

idi527

how could I get the right result?

For exactly this example

iex(1)> [before, _after] = :binary.split("áéíóúx", "x")
["áéíóú", ""]
iex(2)> String.length(before)
5

seems to work.

Most Liked

NobbZ

NobbZ

The index returned is the byte index, and this is perfect for further processing it in parallel to the original input, eg, when cutting the matched substring, as indexing per byte is O(1), per character/glyph is O(n).

Last Post!

adrian

adrian

I didn’t know that existed! it’s the way to go probably

Where Next?

Popular in Questions Top

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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement