单击“筛选”按钮时,我试图根据输入筛选列表框中的数据。
列表中的行采用这种格式:
Id: 1 Leefijd patiënt: 12 Gave blood: yes
所以我的想法是通过循环遍历列表框中的每一行。然后使用正则表达式筛选出数字。
我使用2 regex,因为如果我只过滤数字,我将得到ID和年龄(leeftijd)。
因此,我的第一个正则表达式过滤掉了leeftijd: 2x digets
,第二个正则表达式只删除了文本,只保留了数字。
然后,我用if filtertext执行一个if语句,然后将当前正在循环的整个字符串放在一个应用过滤器的新列表中。
但不知怎么的,整件事都起作用了。它不需要在那里安装过滤器,只需迁移它们,但一旦我尝试过滤,它就会中断。
private void button1_Click(object sender, EventArgs e)
{
string filter = txtFiltered.Text;
int amountOfItemsInList= lstOrgaandonatie.Items.Count;
for (int i = 0; i < amountOfItemsInList; i++)
{
string line= lstOrgaandonatie.Items[i].ToString();
string firstFilter= Regex.Match(line, "Leefijd patiënt:+ \\d{2}").Value;
string finalFilter = Regex.Match(firstFilter, "\\d{2}").Value;
if (finalFilter== filter )
{
lsttest.Items.Add(line);
}
}
}
发布于 2015-06-07 09:53:42
你不需要两个雷格。它们可以这样组合-
string filter = "12";
string line = "Id: 1 Leefijd patiënt: 12 Gave blood: yes";
Regex rx = new Regex(@"Leefijd[ ]patiënt:[ ]+(\d+)");
Match _m = rx.Match( line );
if (_m.Success && _m.Groups[1].Value == filter)
{
Console.WriteLine("Add this to listbox {0} ", line );
}
https://stackoverflow.com/questions/30695836
复制