C# 我只想在我的角色匹配后得到接下来的5个单词

C# 我只想在我的角色匹配后得到接下来的5个单词,c#,regex,C#,Regex,我在C#中使用Regex。我想在我的字符串匹配后得到下一个5个单词 Regex regex = new Regex("Born(.*){0,5}"); string result = regex.Match(UrlString).Value; 首先,我需要找到“出生”这个词。然后,我需要在找到“Born”这个词之后再找到下5个词 例子 输入: 莫迪出生于瓦德纳格尔的古吉拉特邦一个家庭,小时候帮助父亲卖茶,后来经营自己的摊位 结果: 一个古吉拉特邦的家庭 但是我没有得到想要的结果 string

我在C#中使用
Regex
。我想在我的字符串匹配后得到下一个5个单词

Regex regex = new Regex("Born(.*){0,5}");
string result = regex.Match(UrlString).Value;
首先,我需要找到“出生”这个词。然后,我需要在找到“Born”这个词之后再找到下5个词

例子 输入:

莫迪出生于瓦德纳格尔的古吉拉特邦一个家庭,小时候帮助父亲卖茶,后来经营自己的摊位

结果:

一个古吉拉特邦的家庭

但是我没有得到想要的结果

string result = UrlString.Substring(UrlString.IndexOf("Born"), 300);
使用正则表达式:

string result = Regex.Match(UrlString,@"Born(.){300}").ToString())
Regex r=newregex(“Born(?[^]+){5}”);
字符串输入=“asd出生asd asd asd asd asd asd asd asd asd出生qwe qwe qwe qwe qwe rtz rtz”;
foreach(在r.Matches(输入)中匹配m)
{
Console.WriteLine(m.Groups[“words”]值);
}
解释:

(?<words>xxxxxx) // is a group which can be accessed from Matches
[^ ] // All characters except space " "
+ // one or more times
([^ ]+ ) // characters followed by a space => A word
{5} // five words
(?xxxxxx)//是可以从匹配项访问的组
[^]//除空格外的所有字符“”
+//一次或多次
([^]+)//字符后跟空格=>单词
{5} //五个字

请测试用例?请定义“单词”你是指“字符”而不是“单词”吗?但我没有得到想要的结果。你得到了什么结果?你期望得到什么结果?请提供一个简短的示例,说明您实际得到了什么以及您实际期望得到什么。@ParthPandita简单地重复您的问题陈述并不能为我们提供任何其他信息。人们已经问过你问题了,你能解决这些问题吗?我需要使用正则表达式。将长度作为第二个参数,而不是索引。这也会使ArgumentOutOfRange
(?<words>xxxxxx) // is a group which can be accessed from Matches
[^ ] // All characters except space " "
+ // one or more times
([^ ]+ ) // characters followed by a space => A word
{5} // five words