Regular reuse of elixir

I am trying to remove all punctuation from a string using

String.replace(sentence, ~r[\p{P}\p{S}], "") 

However, he does not delete all punctuation marks! As an illustrative example:

 iex(1)> String.replace("foo!&^%$?", ~r[\p{P}\p{S}], "") "foo!?" 

What should i use?

+8
source share
2 answers

You might need / ... / as a delimiters pattern:

 String.replace("foo!&^%$?", ~r/[\p{P}\p{S}]/, "") 

The result can be explained because else [ ] will be used as delimiters in your example, which matches \p{P}\p{S} as a sequence and leads to foo!? (see regex101 example )

Would add in addition + quantifier : ~r/[\p{P}\p{S}]+/

+17
source

If you only work with strings in English, it is easiest and simple to use POSIX character classes :

String.replace("foo!&^%$?", ~r/[[:punct:]]/, "")

0
source

Source: https://habr.com/ru/post/975209/


All Articles