Regex keep w, hyphen between words and remove digits

I struggle to extend following expression to match and remove digits:

[^w -]|_|-(?!w)|(?<!w)-

Example:

123 !"§$%&/()= äüöüÄÖÜÄßßßß hello-123, hello-hello, hello-.

Exprected Output:

äüöüÄÖÜÄßßßß hello hello-hello hello

Answer

You can use

-?d+-?|[^w -]|_|-(?!w)|(?<!w)-
-*d+(?:.d+)?-*|[^w -]|_|-(?!w)|(?<!w)-

See the regex demo.

The -?d+-?| part matches

  • -? – an optional -
  • d+ – one or more digits
  • -? – an optional -
  • | – or (the rest of the alternatives).

The -*d+(?:.d+)?-* part matches float values, too, and matches zero or more hyphens on both ends of the number.

Replace - with W to match any non-word char.

See the Python demo:

import re
text = '123 !"§$%&/()= äüöüÄÖÜÄßßßß hello-123, hello-hello, hello-.'
print( re.sub(r'-?d+-?|[^w -]|_|-(?!w)|(?<!w)-', '', text).strip() )
# => äüöüÄÖÜÄßßßß hello hello-hello hello