如何编写正则表达式来搜索单词中的一组字符 [英] How do i write a regex for searching a set of characters in a word
本文介绍了如何编写正则表达式来搜索单词中的一组字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在字符串值列表中搜索,例如,
[银行A / c,银行OD A / c,手头现金,存款,杂项债务人,杂项债权人,杂项,...]
如果我在搜索框中输入'Ba',它必须将结果集返回给我:
[Bank A / c,Bank OD A / c]
或如果我输入'或',它必须返回给我包含字符串的结果集:
[Sundry Debtors,Sundry债权人]
我尝试过:
1. [_typedName] +
2. _typedName +
3. _typedName *
I need to search among a list of string values, for e.g.,
[Bank A/c, Bank OD A/c, Cash in hand, Deposits, Sundry Debtors, Sundry Creditors, Miscellaneous, ...]
If I type 'Ba' in the search box, it must return me result set as:
[Bank A/c, Bank OD A/c]
or if I type 'or', it must return to me result set containing strings:
[Sundry Debtors, Sundry Creditors]
What I have tried:
1. [_typedName]+
2. _typedName+
3. _typedName*
推荐答案
I强烈建议通过这个
30分钟正则表达式教程 [ ^ ]
I would strongly recommend to go through this
The 30 Minute Regex Tutorial[^]
不是正则表达式。有太多的模式可供匹配。相反,尝试并适应这个例子:
Not Regex. There have too many patterns to match. Instead, try and adapt from this example:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var list = new List<string>();
list.Add("Bank A/cat");
list.Add("Bank OD A/c");
list.Add("Cash in hand");
list.Add("Deposit");
list.Add("Sundry Debtors");
list.Add("Sundry Creditors");
// Your search string here
string search="or";
foreach (string element in list)
{
if (element.IndexOf(search) != -1)
{
Console.WriteLine(element);
}
}
}
}
这篇关于如何编写正则表达式来搜索单词中的一组字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文