Regex.Equal Method Usage


I want to check the entered input is INT/STRING using regex…
I am trying to use

Regex.Equals(vInput,“([0-9])*”) which is returning FALSE in all the cases…

So can any one help me in it…?

May be you should try IsMatch instead of Equals?

1 Like

Is Match will only give the matched string from entered total…Ex:Suppose if u enter …

Regex.Matches(“123a”,“[(0-9)]*”).value will return “123” instead of checking the entered input…
…Here I want to validate the entering input…

In your case

regex.IsMatch(“123a”,“[1]{3}.*$”) -----------> True (First 3 should be integer)

regex.IsMatch(“12aa”,“[2]{3}.*$”) -----------> False (First 3 should be integer)

regex.IsMatch(“123a”,“[3].*$”) -----------> True (First one should be integer)

  1. 0-9 ↩︎

  2. 0-9 ↩︎

  3. 0-9 ↩︎

What it will return?

regex.IsMatch(“123a”,“[1].*$”) -----------> True

regex.IsMatch(“a12a”,“[2].*$”) -----------> False

Hope this helps.(Modified regex)

  1. 0-9 ↩︎

  2. 0-9 ↩︎

1 Like

In My case first one should Return “False”

What is your input and Can you explain the logic why it should return false?

Here I want to check all the entered input is String/Int…
So input1: 123a should be FALSE since it is not a Number…
So thats the reason

Oh understood.

Can you try below

strinput= “123a”

bool isNumber = int32.TryParse(strInput,intVariable)

Ans: False

(just create a intVariable, no need to initialize it)

Thanks for the replies @vvaidya and it will work… But I have followed other way of doing to by Taking that input and convert that to Int…if it throws a exception it is not a number else it is a number…it has worked for me…
var = Convert.ToInt32(input)