0

I am using this exact regex in a RegularExpressionValidator and it works fine. However this code in c# is returning true. Can you please let me know what I am doing wrong?

I am checking for numbers only regex

var x = Regex.IsMatch("1234asdf", @"[0-9]+").ToString();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Ram_P
  • 55
  • 1
  • 6
  • 2
    Of course it returns true. Why shouldn't it? I don't know what that RegularExpressionValidator is you are mentioning, but perhaps that RegularExpressionValidator had a setting (which was enabled) that made it anchoring the regex to the start and end of the string... –  May 30 '19 at 10:04
  • 4
    It matches because the `Regex.IsMatch` API tries to find a match of the pattern in _any_ portion of the input string. If you want to enforce that the entire string input matches, then use the pattern `^[0-9]+$`. This would match an input consisting only of digits. – Tim Biegeleisen May 30 '19 at 10:05
  • 1
    Because it will match the digits only. You have not told it to match the whole string. – ZorgoZ May 30 '19 at 10:06
  • 1
    `[0-9]+` means "one or more digits", and we have `1234` match for the pattern; do you mean `"^[0-9]+$"` pattern? (one or more digits *only*) – Dmitry Bychenko May 30 '19 at 10:06
  • `RegularExpressionValidator` adds anchoring if you didn't anchor it yourself. It will make sure that there are no characters which did not match the regex. In order to do that yourself, you have to add in the anchors, as @Tim said. – canton7 May 30 '19 at 10:07
  • because it matches the value – Kunal Mukherjee May 30 '19 at 10:08
  • You guys are so awesome! Thank you. ASP.NET has a RegularExpressionValidator which you can attach to a text box. The regex I included in the question somehow works just fine when used in that control. But will fix this on the code behind. Thanks again! – Ram_P May 30 '19 at 12:11
  • Thanks @canton7! – Ram_P May 30 '19 at 12:13

4 Answers4

4

Because is finding what you're looking for inside the provided string. You have tyo use ^ and $ to force the whole string to mathc the regex:

^[0-9]+$

^ matches the beginning, $, the end of the string

Please, look ath the Anchors section of this doc: Regular Expression Language - Quick Reference

BTW, instead of [0-9] you should use \d

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Worth mentioning why `RegularExpressionValidator` doesn't need its regex anchoring, as that's half of the question? – canton7 May 30 '19 at 10:08
  • 2
    side note: in c# `\d` means any *unicode* digit (including, say, *persian ones*), `[0-9]` is often a safier option; or put it as `Regex.IsMatch("1234asdf", @"^\d+$", RegexOptions.ECMAScript);` – Dmitry Bychenko May 30 '19 at 10:13
  • There is no `RegularExpressionValidator`. This is an static method of `Regex` class, which has no configuration at all: https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch?view=netframework-4.8#System_Text_RegularExpressions_Regex_IsMatch_System_String_System_String_ – JotaBe May 30 '19 at 10:14
  • I'm assuming he's using the one in System.Web: https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.regularexpressionvalidator?view=netframework-4.8 – canton7 May 30 '19 at 10:16
  • @canton7 I'm assuming nothing: I'm reading the code and I clearly read `Regex.IsMatch`. And that's the method for which I've included the reference link. I'm sorry, but your asusmption is wrong ;) – JotaBe May 30 '19 at 10:19
  • @DmitryBychenko I had never thought of that, and you're right: https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#decimal-digit-character-d " includes the standard decimal digits 0-9 as well as the decimal digits of a number of other character set" – JotaBe May 30 '19 at 10:20
  • I think his question is perfectly clear: "This regex works in RegularExpressionValidator, but not in Regex.Match. Why?". I've added my own answer, anyway. – canton7 May 30 '19 at 10:21
  • @canton7 Now I see your point. – JotaBe May 30 '19 at 10:23
1

Other answers talk about why your regex needs to be anchored. However, nobody's picked up on the RegularExpressionValidator part of your question.

I'm assuming you mean System.Web.RegularExpressionValidator. RegularExpressionValidator does more than just checking whether the regex matches - it also checks whether the regex is an exact match. That is, it effectively adds the ^ and $ anchoring for you.

The code:

Match m = RegexUtil.Match(controlValue, ValidationExpression, RegexOptions.None, MatchTimeout);
return(m.Success && m.Index == 0 && m.Length == controlValue.Length);

Note how it checks m.Success, but also checks that the match starts at the beginning of the value being checked, and ends at the end of the value being checked.

(System.ComponentModel.DataAnnotations.RegularExpressionAttribute does the same thing).

canton7
  • 37,633
  • 3
  • 64
  • 77
0

Because you trying to match one or more digit from 0 to 9. Explaininh:

[0-9] //Match digit 0-9 you can also use /d-(Match any digit char)
+ //Match one or more of the preceding token

If you have some digit in you string, this code:

var x = Regex.IsMatch("1234asdf", @"[0-9]+").ToString();

will always return True. For exaple if you try this one:

var x = Regex.IsMatch("asdf", @"[0-9]+").ToString();

will return False

gtsonkov
  • 91
  • 1
  • 3
  • 9
-1

You have to use ^ and $ to force the whole string to match the regex pattern

^[0-9]+$

^ matches the beginning, $ the end of the string

In C# Write the expression like below format:

var x = Regex.IsMatch("1234", @"^[0-9]+$").ToString();
A.M. Patel
  • 334
  • 2
  • 9