Hi,
I need a simple help... can any one tel me in regular expression how can check only number of chracter..
Eg: hav to print first 3 character. if print 4th character it shuld be error.
Hi,
I need a simple help... can any one tel me in regular expression how can check only number of chracter..
Eg: hav to print first 3 character. if print 4th character it shuld be error.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Jan 24, 2008, 6:03 AM
I can't imagine why you'd want to do this using RE when you can just use string.Length but the following code should count all characters (including '\n') in a given string:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string s = "The quick brown fox jumped over the lazy dog. 7#\r\n";
Regex r = new Regex (".|\n"); // '.' matches any character except '\n'
MatchCollection mc = r.Matches(s);
Console.WriteLine(mc.Count); // 50
Console.ReadLine();
}
}