正则表达式用于获取字符串之前的文本
本文关键字:文本 字符串 用于 获取 正则表达式 | 更新日期: 2023-09-27 18:32:32
我在 c# 中有字符串文本,我试图在表达eachDELETEDDELETED
之前获取所有单词,我想将它们转换为列表或数组。有人可以帮助正则表达式部分。
文本示例: word word word word word word 20 word-16S eachDELETEDDELETEDword word word word word word 20 word-16z1 (size 26), eachDELETEDDELETED
Regex ry = new Regex(@"eachDELETEDDELETED");
MatchCollection matchList = Regex.Matches(extracted, ry.ToString());
var list = matchList.Cast<Match>().Select(match => match.Value).ToList();
foreach (string s in list)
{
richTextBox2.Text += s + "'n";
}
只需使用 Regex.Split:
string example = "word word word word word word 20 word-16S eachDELETEDDELETEDword word word word word word 20 word-16S eachDELETEDDELETED";
string[] parts = Regex.Split(example, "eachDELETEDDELETED");
foreach(string part in parts)
{
Console.WriteLine(part);
}
将输出:
word word word word word word 20 word-16S
word word word word word word 20 word-16S
仅供参考,这也可以通过前瞻和'G
锚来完成:
^|'G(?:([-'w]+)'s*)(?!(?:eachDELETEDDELETED))
查看有关 regex101.com 的演示。