What's the difference between ^([1-9]{1,2},){4}[1-9]{1,2}$ and ([1-9]{1,2},){4}[1-9]{1,2}?

c#, regex

Solution

This is the syntax diagram for `^([1-9]{1,2},){4}[1-9]{1,2}$`

This is the syntax diagram for `([1-9]{1,2},){4}[1-9]{1,2}`

You can see the difference between the 2 regular expressions is the first regular expression forces to match from `Start` of the string to `End` of the string.

The diagrams are generated using Regexper.

Problem

If I use `([1-9]{1,2},){4}[1-9]{1,2}` to match the input. `1,2,3,4,5,6,7,8,9` would be matched as a valid input. But, if I add ^ at the beginning and $ at the end. The expression `^([1-9]{1,2},){4}[1-9]{1,2}$` would match `1,2,3,4,5` as I desired. My question is: What's the difference between `^([1-9]{1,2},){4}[1-9]{1,2}$` and `([1-9]{1,2},){4}[1-9]{1,2}` ? You know, I only add ^ and $. Could you explain why does the difference happen?

Original source