我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

狐的傳說
TA貢獻1804條經驗 獲得超3個贊
string result = UrlString.Substring(UrlString.IndexOf("Born"), 300);
使用正則表達式:
string result = Regex.Match(UrlString,@"Born(.){300}").ToString())
- 2 回答
- 0 關注
- 102 瀏覽
添加回答
舉報
0/150
提交
取消