How to remove all non word characters except ä,ö and ü from a text using RegExp

Viewed 292

I have a file and I want to remove all non-word characters from it, with the exception of ä, ö and ü, which are mutated vowels in the German language. Is there a way to do word.gsub!(/\W/, '') and put exceptions in it?

Example:

text = "übung bzw. äffchen"
text.gsub!(/\W/, '').

Now it would return "bungbzwffchen". It deletes the non word characters, but also removes the mutated vowels ü and ä, which I want to keep.

2 Answers

You may be able to define a list of exclusions by using some kind of negative-lookback thing, but the simplest I think would be to just use \w instead of \W and negate the whole group:

word.gsub!(/[^\wÄäÖöÜü]/, '') 

You could also use word.gsub(/[^\p{Letter}]/, ''), that should get rid of any characters that are not listed as "Letter" in unicode.

You mention German vowels in your question, I think it's worth noting here that the German alphabet also includes the long-s : ẞ / ß

Update:

To answer your original question, to define a list of exclusions, you use the "negative look-behind" (?<!pat):

word.gsub(/\W(?<![ÄäÖöÅåẞß])/, '')

You could use the && operator within a character class:

text = "übung bzw. äffchen  ÄÖÜ"

text.gsub(/[\W&&[^äÄöÖüÜ]]/, '')
  #=> "übungbzwäffchenÄÖÜ"

The regular expression reads, "match a character in the set of characters formed by intersecting the set of all non-word characters with the set of all characters other than those in the string "äÄöÖüÜ". See Regexp (search for "&& operator").

Related