简体   繁体   English

c#正则表达式不止一个词

[英]c# Regex more than one word

I have a text for example , Nim-Qloth, Iceberg, Szatan Krul, Consequence, Arithael, and I use regex to find text between , , I have that code我有一个文本,例如 Nim-Qloth、Iceberg、Szatan Krul、Consequence、Arithael,我使用正则表达式在, ,之间查找文本, ,我有那个代码

using (StreamReader sr = new StreamReader(miejscepliku))
{
    String line = sr.ReadToEnd();
    String odbiorca = Regex.Match(line, @"\, ([^,]*)\,").Groups[1].Value;
    textBox3.Text = odbiorca;
}

My code find one word and word is showing in textbox3 word is "Nim-Qloth" but i want click second time and find next word ex.我的代码找到一个单词,文本textbox3显示的单词是"Nim-Qloth"但我想第二次单击并找到下一个单词 ex。 "Iceberg" . "Iceberg" How do that?那怎么办?

String.Split is sufficient and more efficient: String.Split就足够了,而且效率更高:

String[] odbiorca = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

You can access an array by index(zero based).您可以通过索引(从零开始)访问数组。 You just need to store the current index so that you can show the next on each button-click.您只需要存储当前索引,以便您可以在每次单击按钮时显示下一个索引。

private int currentIndex = -1; // a field in your class

...

if(++currentIndex == odbiorca.Length) currentIndex = 0;
string currentWord = odbiorca[currentIndex];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM