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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Jan 24, 2008, 1:03 AM EST
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();
}
}