WebWanderer
WebWanderer

Reputation: 10887

Regex Class With Exceptions

I know that the regex class \D matches "all characters that are non-numeric" but I would like to match on all characters that are non-numeric and are not / or -

How might I do this? Thanks!

Upvotes: 1

Views: 192

Answers (2)

Ryszard Czech
Ryszard Czech

Reputation: 18631

You already know how to find non-numeric characters with \D. You may lay a restriction on the \D to exclude / and - and any other non-numeric characters with a negative lookahead:

(?![\/-])\D

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [\/-]                    any character of: '\/', '-'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  \D                       non-digits (all but 0-9)

Upvotes: 0

ndnenkov
ndnenkov

Reputation: 36110

You can negate character sets by putting ^ inside:

[^\d\/-]

Will match any one character, which is not a digit, forward slash or dash.

Upvotes: 3

Related Questions