J'ai le texte de chaîne en c # que j'essaie d'obtenir tous les mots avant mon expression eachDELETEDDELETED
et je veux les transformer en une liste ou un tableau. Quelqu'un peut aider avec la partie regex.
échantillon de texte:
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";
}
Réponses:
1 pour la réponse № 1Il suffit d'utiliser 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);
}
Est-ce que la sortie:
word word word word word word 20 word-16S
word word word word word word 20 word-16S
0 pour la réponse № 2
Juste pour référence, cela peut aussi être fait avec un look d'avant et le G
ancre:
^|G(?:([-w]+)s*)(?!(?:eachDELETEDDELETED))
Voir une démonstration sur regex101.com.