Relative Content

Tag Archive for c#regex

Why does Regex.IsMatch return true for a newline as input when expecting zero to 4 digits?

I’m trying to validate some user input for the country code of a phone number. The country code is optional, but when it contains a value it should be 1 to 4 digits. I wrote a regular expression and expected the regular expression to fail when passing "n" in C#. It appeared to match, so I popped open the C# interactive console in Visual Studio 2022 (version 17.8.6). Indeed, the pattern was matching a newline character unexpectedly. My experiments below:

How to get numbers

myPattern = @”?:[.]?[d]+(?:[,.]*d)*”; However, I would like to exclude the numbers following the dollar sign ($), and want to exclude numbers in parentheses (){}[]. For example: input text (match) —————————– abc123,000.5xyz (match:123,000.5) abc.5xyz (match:.5) abc0.2xyz (match:0.2) abc123.2xyz (match:123.2) abc$123xyz (false) abc(123)77xyz (match:77) abc12{123}xyz (match:12) abc[123]xyz (false) c# regex You can use this regex: (?<![${([d])d+(,d{3})*(.d+)?(?![]})]) It […]