Regex.Equal Method Usage


#1

Hi,

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…?


#2

May be you should try IsMatch instead of Equals?


#3

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…


#4

In your case

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

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

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


#5

What it will return?


#6

regex.IsMatch(“123a”,"^[0-9].*$") -----------> True

regex.IsMatch(“a12a”,"^[0-9].*$") -----------> False

Hope this helps.(Modified regex)


#7

In My case first one should Return “False”


#8

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


#9

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


#10

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)


#11

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)