belgoros

belgoros

Regex question for hyphen match

I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference of matching in Javascript, Java, and Ruby (that’s why I’m a kind of stuck).
So I’d like to split a String input on one of the below characters:

  • spaces
  • underscores
  • comma
  • colon
  • punctuation
  • special characters

So I have the following code snippet in iex session:

String.split("testing, one, two car : carpet as java : javascript!!&@$%^& co-operation one_two 1 2", ~r/[\W-[_]|:]+/u, trim: true)

that returns:

["testing", "one", "two", "car", "carpet", "as", "java", "javascript", "co",
 "operation", "one", "two", "1", "2"]

I had to substract underscore symbol from \W(any “non-word” character) as it includes it (a-zA-Z_).
As you see, there is still a problem with matching the hyphen in co-operation word.
Whatever I try, whenever I put - in the above regex, nothing works, - it just breaks the previously matching cases.

Any ideas? Thank you.

Marked As Solved

blatyo

blatyo

Conduit Core Team

Try this: ~r/([^\w\-]|_)+/u

Also Liked

blatyo

blatyo

Conduit Core Team

Oh, I finally understand what you’re doing. Use this regex instead.

String.split("co-operation tree_five one two 1 2 azerty : double:side", ~r/[^a-zA-Z0-9\-]+/u, trim: true)
#=> ["co-operation", "tree", "five", "one", "two", "1", "2", "azerty", "double",
 "side"]

\W-[_] doesn’t mean subtract the underscore from the \W character class. There is no such supported operation in a regex like that which I’m aware of.

NobbZ

NobbZ

I’m not sure what you mean by “substracting the underscore”.

When I simply use ~r/[\W_]/ as the splitter, I get the same result as you with your more complicated regex. It seems as if it were already splitting on -. this way.

blatyo

blatyo

Conduit Core Team

If I understand your problem, I think you need to escape - in your character class. The reason is that - is used to defined ranges of characters. For example, ~r/[a-z]/ means all characters from a to z, not a, -, and z. You can escape characters in a character class, by using \. So, for the previous example, to get it to mean a, -, and z, you’d do ~r/[a\-z]/.

Hope that helps

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement