Md Kamruzzaman Sarker
Md Kamruzzaman Sarker

Reputation: 2407

Regular expression to string or int conversion

I want to convert the matched expression in string or int. But in .NET Framework I do not find any method to do this. I've tried this

s=+OK 58 exists;
var m = Regex.Match(s, @"\+OK (?<totalemail>[0-9]+)");
        Console.WriteLine("Total Email: " + m.Groups["totalemail"].Value);
        string s1= Convert.ToString(m.Groups["totalemail"].Value);
        Console.WriteLine(s1);

This first writeline prints 58 and the second WriteLine() call prints nothing that means s1="".

If I use int conversion like this

int s=Convert.ToInt32(m.Groups["totalemail"].Value);

then it shows error

Input String is not in correct format.

Is it possible to convert matched regular expression to string or int? if possible please give help. Thanks in advance.

Upvotes: 2

Views: 1795

Answers (2)

Md Kamruzzaman Sarker
Md Kamruzzaman Sarker

Reputation: 2407

I have solved it. Solution will be like this--

  s=+OK 58 exists;

  var m = Regex.Match(s, @"\+OK (?<totalemail>[0-9]+)");
        Console.WriteLine("Total Email: " + m.Groups["totalemail"].Value);
        int index = m.Groups["totalemail"].Index;
        int length = m.Groups["totalemail"].Length;
        Console.WriteLine(index + "  " + length);
        string str;
       Console.WriteLine(str=m.ToString().Substring(index,length));
        int i;
       Console.WriteLine(i = Convert.ToInt32(str));

Upvotes: 1

ruakh
ruakh

Reputation: 183504

You have a typo. This:

"totalemail]"

should be this:

"totalemail"

Upvotes: 3

Related Questions