0

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)

2
  • You aren’t using “delimiter” but meaning “separator”? Or are you? QUOTE: delimiter: A “character” or “string” that sets bounds to an arbitrarily-sized textual object, not to be confused with a “separator” or “terminator”. “To delimit” really just means “to surround” or “to enclose” (like these parentheses are doing).
    – tchrist
    Nov 8, 2010 at 16:26
  • Sure, "separator". Sorry for my English :)
    – manuna
    Nov 8, 2010 at 16:48

2 Answers 2

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

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

^[-.,\s]{0,2}(?:\d[-.,\s]{0,2}){9}$
1
  • Right, after posting the question I've come to something similar, partly like: (\d[ \-]{0,1}){11,13} (in my case ^ and $ are not required, as the search goes through a piece of text) Still, I wonder, what "(?: )" usage adds to this expression? Is it generally preferred to "( )" in case we need no backreferences?
    – manuna
    Nov 8, 2010 at 16:45
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.