Hilary
Hilary

Reputation: 485

Regex to remove numbers but keep numbers that are combined with a hyphen

I need to write a regex pattern that will remove everything from my text except letters, hyphen (-), slash (/) (e.g., '[^a-zA-Z-/]') and numbers in combination with a hyphen ('5-', '-123'). Single numbers or numbers in combination with other characters should be removed, so '9-SomeWord', 'SomeWord-34' must be kept, but '456ml', '23' or '56%' should be removed.

What should be the regex pattern?

Upvotes: 0

Views: 355

Answers (1)

MikeM
MikeM

Reputation: 13631

Try

r'[^\w/-]+|_|(?<![\d-])\d+(?!\d*-)'

See regex101 for testing and further details.

Upvotes: 1

Related Questions