0

Let's say the phone number has to be three numbers long. We can do it like \d\d\d or \d{3}. This is clear to me. We also can say it has to start with three numbers like ^\d{3}. And that it ends to three numbers with \d{3}$. And both with ^\d{3}$. So far this is clear to me. But the thing I fail to understand is that why this does not match: "123asd456"? It matches the rules, because 1) It starts with three digits and 2) it ends to three digits. What part there says that "yeah but the LENGTH has to be exactly three"?

And yeah, I tested (with c#) that it fails to match, I just don't understand why.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • 4
    `^` = start, `\d{3}`=exactly three digits, `$`=end. There is no space for letters in there. – Andrew Morton Jul 10 '18 at 12:21
  • 2
    `^` - start of string, `\d{3}` - 3 digits, `$` - end of string. So, only a string containing 3 digits can be matched. To match 3 diigts at the start or end, use `^\d{3}|\d{3}$` – Wiktor Stribiżew Jul 10 '18 at 12:21
  • So if I use both "starts with" and "ends with", then Regular Expression is interpreted as "length" or "and nothing else"? Can I then somehow check that "starts with three digits, followed by possibly whatever and ends with three digits"? Is that just ^\d{3}.*\d{3}$ or do we have something simpler for that? – Arto Kilponen Os Kilponen Jul 10 '18 at 12:55

0 Answers0