Davids said:
can't see how regex could make a match only after the 300th word?
I found it. (What I learned from all this was that more or less the full
power and Perl's regular expressions has been put into C# without changing
their syntax, and that the way to use them in C# is of course less elegant
than in Perl, but quite logical once you got through all those classes
involved.
Use the regex ^((?:\W+\w+){100}).*
with the replacement pattern $1
The (?: ... ) defines a non-capturing group, multiplied by {100} to capture
100 words.
It is enclosed in a capturing group ( ... ) that defines $1 as the first
captured part (which consists of the first 100 words).
The complete program (with 5 instead of 100 words):
//////////////////////////////////////////////////////////////
using System;
using System.Text.RegularExpressions;
class MainClass
{
public static void Main(string[] args)
{
string texample =
@" abc,; def... : ghi jkl ! mno pqr stu vwx yz";
Regex rexample = new Regex(@"^((?:\W+\w+){5}).*");
string replaced = rexample.Replace(texample, "$1");
Console.WriteLine(texample);
Console.WriteLine(replaced);
}
}
//////////////////////////////////////////////////////////////
The output:
abc,; def... : ghi jkl ! mno pqr stu vwx yz
abc,; def... : ghi jkl ! mno
Joachim