简体   繁体   English

如何使用 Regex.Match 执行部分搜索并返回完整单词?

[英]How to perform partial search and return the full word with Regex.Match?

I'm trying to handle my exceptions based on regular expressions.我正在尝试根据正则表达式处理我的异常。 To do this, all my constraints start the same way.为此,我所有的约束都以相同的方式开始。 (CCC_TABLE_COLUMN_TYPECONSTRAINT) (CCC_TABLE_COLUMN_TYPECONSTRAINT)

Ex:前任:

CCC_USER_NAME_UNIQUE CCC_USER_NAME_UNIQUE

CCC_BOOKS_TITLE_UNIQUE. CCC_BOOKS_TITLE_UNIQUE。

I'm not able to return the entire string.我无法返回整个字符串。

try
{
      await DbContext.SaveChangesAsync().ConfigureAwait(false);
}catch(Exception ex)
{
      regex = new Regex("CCC");
      var reg1 = regex.Match(ex.toString());         
} 

In reg1 only "CCC" is returned.在 reg1 中,仅返回“CCC”。 How do I return the entire constraint name?如何返回整个约束名称?

You need to include matching all the parts您需要包括匹配的所有部分

CCC_[A-Z]+_[A-Z]+_[A-Z]+

Assuming you are only using upper case letters in the names.假设您只在名称中使用大写字母。 If not replace the [AZ] with an expression that would match.如果不使用匹配的表达式替换[AZ]

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

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