如何解决Regex.Matches的问题? [英] How to solve the problem of Regex.Matches ?

查看:100
本文介绍了如何解决Regex.Matches的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下代码:



 List< string> ListOfGlobalTables =  new  List< string>(); 
string selectpart = SELECT Summary_New.ServiceName AS'[Q1]。[ServiceName]'


ListOfGlobalTables.Add( 摘要);
ListOfGlobalTables.Add( Summary_New);

foreach string item in ListOfGlobalTables)
{
string tableNamePlusDot = item + ;

int count = Regex.Matches(selectpart,tableNamePlusDot).Count;

// 只考虑应返回的数量
}





对于List中的第一项,以下代码将tableNamePlusDot作为Summary返回。

< pre lang =c#> string tableNamePlusDot = item +





之后,对于以下代码,计数应返回0



  int  count = Regex.Matches(selectpart,tableNamePlusDot).Count; 



但它返回1!

任何人都可以帮我解决这个问题吗?

解决方案

而不是添加'。'add

 '\。'

到模式


。正则表达式匹配任何字符。

如果你想将搜索限制为摘要加上一个点,那么:

  string  tableNamePlusDot = item +  @  \。; 

应该修复它。


Please see the following code:

List<string> ListOfGlobalTables = new List<string>();
string selectpart="SELECT Summary_New.ServiceName AS '[Q1].[ServiceName]'"


ListOfGlobalTables.Add("Summary");
ListOfGlobalTables.Add("Summary_New");

 foreach (string item in ListOfGlobalTables)
                {
                    string tableNamePlusDot = item + ".";

                    int count = Regex.Matches(selectpart, tableNamePlusDot).Count; 
                   
                  // just think what count should return
                  }



For the first item in the List, the following code return tableNamePlusDot as "Summary.".

string tableNamePlusDot = item + ".";



After that the count should return 0 for the following code

int count = Regex.Matches(selectpart, tableNamePlusDot).Count;


but it returns 1 !
Can anyone please help me to solve this problem ?

解决方案

instead of adding '.' add

'\.'

to the pattern


"." in a regex matched any character.
If you want to limit the search to "Summary" plus a dot, then:

string tableNamePlusDot = item + @"\.";

Should fix it.


这篇关于如何解决Regex.Matches的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆