manuna
manuna

Reputation: 861

Regexp of certain length ignoring specified characters

Assume we have some string of defined length, that could contain some delimiters, that are used only for user-friendly view.

Example: 9-digit string, "123456789", that could be presented like "123,456,789" or "123.456.789" or "1-234-5-67-89" or "123 456 789"

What I need, is a regexp, that could count length ignoring that delimiters. Something like [\d|,\.\-\s]{9}, but with only \d counting into {9} (any number of delimiter characters allowed... optionally, delimiter characters shouldn't exceed two in a row)

Upvotes: 0

Views: 1929

Answers (2)

Gumbo
Gumbo

Reputation: 655269

Instead of bothering my head about finding an appropriate regular expression that might do this, I would rather use more simple regular expressions to either:

  • find all digits and check the number of matches, or
  • remove all non-digits and check the length of the remaining.

Upvotes: 0

kennytm
kennytm

Reputation: 523304

^[-.,\s]*(?:\d[-.,\s]*){9}$

optionally, delimiter characters shouldn't exceed two in a row:

^[-.,\s]{0,2}(?:\d[-.,\s]{0,2}){9}$

Upvotes: 6

Related Questions