为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
函数式编程 2022-12-04 10:36:01
我Regex在 C# 中使用。我想在我的字符串匹配后得到接下来的 5 个单词。Regex regex = new Regex("Born(.*){0,5}"); string result = regex.Match(UrlString).Value;首先,我需要找到“Born”这个词。然后我需要在找到“Born”这个词后得到接下来的 5 个词。例子输入:例子——莫迪出生在瓦德纳加尔的一个古吉拉特语家庭,小时候帮助父亲卖茶,后来经营自己的摊位。结果:到古吉拉特语家庭但我没有得到想要的结果。
查看完整描述

2 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

Regex r = new Regex("Born (?<words>([^ ]+ ){5})");

string input = "asd Born asd asd asd asd asd asd asd asd asd asd Born qwe qwe qwe qwe qwe rtz rtz rtz";


foreach (Match m in r.Matches(input))

{

    Console.WriteLine(m.Groups["words"].Value);

}

解释:


(?<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


查看完整回答
反对 回复 2022-12-04
?
狐的传说

TA贡献1804条经验 获得超3个赞

string result = UrlString.Substring(UrlString.IndexOf("Born"), 300);

使用正则表达式:

string result = Regex.Match(UrlString,@"Born(.){300}").ToString())


查看完整回答
反对 回复 2022-12-04
  • 2 回答
  • 0 关注
  • 55 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信