Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ Contains-如何确保特定字符不跟随_C#_Regex_Linq - Fatal编程技术网

C# 使用LINQ Contains-如何确保特定字符不跟随

C# 使用LINQ Contains-如何确保特定字符不跟随,c#,regex,linq,C#,Regex,Linq,我有以下linq: var s = string.Join(",", products.Where(p => p.Query.Contains(tableName)) .Select(p => p.ProductId.ToString())); 我正在搜索一个列表,但我想省略表名与表名不符的项目。确切地说,有些表的末尾有一个额外的数字,希望省略这些数字 例如,如果我在一个字符串中搜索table1,它不应该包括tabl

我有以下linq:

var s = string.Join(",", products.Where(p => p.Query.Contains(tableName))
                                 .Select(p => p.ProductId.ToString()));
我正在搜索一个列表,但我想省略表名与表名不符的项目。确切地说,有些表的末尾有一个额外的数字,希望省略这些数字

例如,如果我在一个字符串中搜索table1,它不应该包括table10

上述查询数据将包括以下内容:

包括:

this is a test table1 found
This is another test table1(some other data here)
thistable1shouldbeincluded
不包括

this is a text table10 found
table10is here
testing this should not be included table18(testing)
基本上,检查是为了确保数字0-9不符合contains语句中的搜索条件。其他任何事情都可以接踵而至。如果可能的话,我想让它成为同一个linq的一部分,并且尽可能简单。谢谢你的帮助

试试正则表达式:
table1(?!\d)

尝试正则表达式:
table1(?!\d)


当您的
表格
没有以数字结尾时,如
表格
,是否要在此处返回类似
表格1的条目
?这里不清楚单词的边界是什么。你可能想用正则表达式来做this@Rango“说得好!Wiktor,谢谢你的意见。无论表格名称是否以数字结尾,Matt给出的以下答案都是正确的。如果您的
表格
没有以数字结尾,如
表格
,是否要在此处返回类似
表格1的条目
?这里不清楚单词的边界是什么。你可能想用正则表达式来做this@Rango“说得好!Wiktor,谢谢你的意见。无论表格名称是否以数字结尾,Matt给出的以下答案都正确。
var s = string.Join(",", products.Where(p => Regex.IsMatch(p.Query, $@"{tableName}(?!\d)"))
                                 .Select(p => p.ProductId.ToString()));