egor

egor

How to port this regular expression from JavaScript

Hi all, I need to port this from JavaScript to Elixir:

/[\0-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/

When I try:

Regex.compile!("[\0-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]", "u")

I get:

** (ArgumentError) invalid or reserved Unicode codepoint 55296
    (elixir) src/elixir_interpolation.erl:200: :elixir_interpolation.append_codepoint/5
    (elixir) src/elixir_interpolation.erl:81: :elixir_interpolation."-unescape_tokens/2-lc$^0/1-0-"/2
    (elixir) src/elixir_interpolation.erl:81: :elixir_interpolation.unescape_tokens/2
    (elixir) src/elixir_tokenizer.erl:673: :elixir_tokenizer.handle_strings/6
    (elixir) lib/code.ex:669: Code.string_to_quoted/2

and when I try

~r/[\0-\x{D7FF}\x{E000}-\x{FFFF}]|[\x{D800}-\x{DBFF}][\x{DC00}-\x{DFFF}]|[\x{D800}-\x{DBFF}](?![\x{DC00}-\x{DFFF}])|(?:[^\x{D800}-\x{DBFF}]|^)[\x{DC00}-\x{DFFF}]/u

I get

** (Regex.CompileError) disallowed Unicode code point (>= 0xd800 && <= 0xdfff) at position 39
    (elixir) lib/regex.ex:172: Regex.compile!/2
    (elixir) expanding macro: Kernel.sigil_r/2
    iex:43: (file)

Looks like the problem is with \uD800. Does anybody has an idea how to solve it?

Most Liked

Nicd

Nicd

Looks like you are trying to find surrogate pairs? Elixir uses UTF-8 strings and that is AFAIK not allowed to contain surrogate pairs, so binaries containing them would not be valid strings. And since Regex operates on Elixir strings, it does not make sense to ask it to find them.

What is the actual thing you are trying to accomplish here?

egor

egor

Thanks! Looks good, but too simple for my case (doesn’t support urls with http://, internationalized domain names, etc).

OvermindDL1

OvermindDL1

Perhaps a set of PR’s to buff it up could be useful? It could be a great generic library for handling such things. :slight_smile:

OvermindDL1

OvermindDL1

Ooo very cool, hope it gets accepted soon!

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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 30048 115
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
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
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement