Using a regular expression, I would like to match the following string:
4232,2232
I have tried
^[0-9]+(,[0-9]+)$
However, it doesn't work as expected. I want to cater for 4 numbers, a comma and 4 numbers.
Using a regular expression, I would like to match the following string:
4232,2232
I have tried
^[0-9]+(,[0-9]+)$
However, it doesn't work as expected. I want to cater for 4 numbers, a comma and 4 numbers.
You can use the following:
\d{4},\d{4} //or ^\d{4},\d{4}$ with anchors for start and end of string
Explanation:
\d{4}
match digits exactly 4 times (\d
is shorthand notation for [0-9]
),\d{4}
followed by comma and exactly 4 digits againIf i understand you correctly the RegEx you are looking for is basically:
(\d{4},\d{4})
This matches your provided expression as one group. As an alternative you could write:
([0-9]{4},[0-9]{4})
which has the same result.
In C#, you can use Regex.IsMatch
with the \b\d{4},\d{4}\b
regex:
var found_value1 = Regex.IsMatch("4232,2232", @"\b\d{4},\d{4}\b");
var found_value2 = Regex.IsMatch("12345,2232", @"\b\d{4},\d{4}\b");
\b
makes sure we match whole number.
Output:
true
false