Nate Pet
Nate Pet

Reputation: 46222

Regex c# involving digit and spaces

What does the following Regex do?

    \d{1,3}.?\d{0,3}\s[0-9a-zA-Z. -]{4,40} 

I understand d is for digit but what does 1,3 do.

If somebody can explain further as well that would be appreciated

Upvotes: 2

Views: 516

Answers (1)

fge
fge

Reputation: 121712

{n,m} is a quantifier which means "at least n times, at most m times". Like all quantifiers, it is greedy by default, and for regex dialects which support them, it also has lazy and possessive versions ({n,m}? and {n,m}+ respectively -- .NET supports the former but not the latter, unfortunately).

If n is not specified, it is 0; if m is not specified, it is infinity.

This means you can "rewrite" the classical *, + and ? using this quantifier:

  • * is {0,};
  • + is {1,};
  • ? is {0,1}.

(note: I think the . in .? was meant to be a literal dot, which means it should be escaped, that is \.?; the dot in a regex means "any character", except in a character class.)

As to the regex itself:

\d{1,3}        # match a digit, one to three times, followed by
.?             # any character, 0 or one time (see my remark), followed by
\d{0,3}        # a digit, zero to three times, followed by
\s             # a space character, followed by
[0-9a-zA-Z. -] # a digit, or any letter, or a dot, or a space, or a hyphen,
{4,40}         # 4 to 40 times

Finally, it should be noted that \d in .NET languages does not limit itself to 0-9, it can match other Unicode digits.

edit: the regex, fixed, taking into account @AlanMoore's comment, would be:

\d{1,3}(\.\d{1,3})?\s[0-9a-zA-Z. -]{4,40}

Maybe this regex should be anchored, too... But this is a guess only.

Upvotes: 2

Related Questions