C# 正则表达式匹配子字符串

C# 正则表达式匹配子字符串,c#,regex,C#,Regex,我尝试创建一个正则表达式,它可以提取匹配的所有内容: [aA-zZ]{2}[0-9]{5} 问题是,当我有如ABCD12345678时,我想从匹配中排除 有人能帮我解决这个问题吗 编辑1: 我在字符串中查找两个字母和五个数字,但当我有像ABCD12345678这样的字符串时,我想排除匹配,因为当我使用上述正则表达式时,它将返回CD12345 编辑2: 我并没有检查所有的东西,但我想我找到了答案: 当字段为空时,则为字段 当fnRegExMatch(字段'[a-zA-Z]{2}[0-9]{5}'

我尝试创建一个正则表达式,它可以提取匹配的所有内容:

[aA-zZ]{2}[0-9]{5}
问题是,当我有如ABCD12345678时,我想从匹配中排除

有人能帮我解决这个问题吗

编辑1: 我在字符串中查找两个字母和五个数字,但当我有像ABCD12345678这样的字符串时,我想排除匹配,因为当我使用上述正则表达式时,它将返回CD12345

编辑2: 我并没有检查所有的东西,但我想我找到了答案:

当字段为空时,则为字段
当fnRegExMatch(字段'[a-zA-Z]{2}[0-9]{5}')='N/a'时,则该字段
当像“[^a-z][a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9]”这样的字段或像“[a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-

ELSE字段

第一个
[aA zZ]
没有任何意义,第二个使用单词边界:

\b[a-zA-Z]{2}[0-9]{5}\b
您还可以使用不区分大小写的修饰符:

(?i)\b[a-z]{2}[0-9]{5}\b
根据你的评论,看起来你可能在五位数后加了下划线。在这种情况下,单词边界不起作用,您必须使用ths:

(?i)(?<![a-z])([a-z]{2}[0-9]{5})(?![0-9])
(?i)(?
(?是一个假设在两个必填字母之前没有字母的字母

(?![0-9])
是一种假设你在五个数字之后没有一个数字是必须的

第一个
[aA zZ]
没有任何意义,第二个使用单词边界:

\b[a-zA-Z]{2}[0-9]{5}\b
您还可以使用不区分大小写的修饰符:

(?i)\b[a-z]{2}[0-9]{5}\b
根据您的评论,五位数字后似乎有下划线。在这种情况下,单词边界不起作用,您必须使用ths:

(?i)(?<![a-z])([a-z]{2}[0-9]{5})(?![0-9])
(?i)(?
(?是一个假设在两个必填字母之前没有字母的字母

(?![0-9])
是一种假设你在五个数字之后没有一个数字是必须的

第一个
[aA zZ]
没有任何意义,第二个使用单词边界:

\b[a-zA-Z]{2}[0-9]{5}\b
您还可以使用不区分大小写的修饰符:

(?i)\b[a-z]{2}[0-9]{5}\b
根据您的评论,五位数字后似乎有下划线。在这种情况下,单词边界不起作用,您必须使用ths:

(?i)(?<![a-z])([a-z]{2}[0-9]{5})(?![0-9])
(?i)(?
(?是一个假设在两个必填字母之前没有字母的字母

(?![0-9])
是一种假设你在五个数字之后没有一个数字是必须的

第一个
[aA zZ]
没有任何意义,第二个使用单词边界:

\b[a-zA-Z]{2}[0-9]{5}\b
您还可以使用不区分大小写的修饰符:

(?i)\b[a-z]{2}[0-9]{5}\b
根据您的评论,五位数字后似乎有下划线。在这种情况下,单词边界不起作用,您必须使用ths:

(?i)(?<![a-z])([a-z]{2}[0-9]{5})(?![0-9])
(?i)(?
(?是一个假设在两个必填字母之前没有字母的字母

(?![0-9])
是一种假设您在五个数字之后没有一个数字是必需的

这将是代码以及使用示例

public static Regex regex = new Regex(
          "\\b[a-zA-Z]{2}\\d{5}\\b",
    RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );



//// Replace the matched text in the InputText using the replacement pattern
// string result = regex.Replace(InputText,regexReplace);

//// Split the InputText wherever the regex matches
// string[] results = regex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = regex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = regex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = regex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = regex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = regex.GetGroupNumbers();

这将是代码以及使用示例

public static Regex regex = new Regex(
          "\\b[a-zA-Z]{2}\\d{5}\\b",
    RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );



//// Replace the matched text in the InputText using the replacement pattern
// string result = regex.Replace(InputText,regexReplace);

//// Split the InputText wherever the regex matches
// string[] results = regex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = regex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = regex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = regex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = regex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = regex.GetGroupNumbers();

这将是代码以及使用示例

public static Regex regex = new Regex(
          "\\b[a-zA-Z]{2}\\d{5}\\b",
    RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );



//// Replace the matched text in the InputText using the replacement pattern
// string result = regex.Replace(InputText,regexReplace);

//// Split the InputText wherever the regex matches
// string[] results = regex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = regex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = regex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = regex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = regex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = regex.GetGroupNumbers();

这将是代码以及使用示例

public static Regex regex = new Regex(
          "\\b[a-zA-Z]{2}\\d{5}\\b",
    RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );



//// Replace the matched text in the InputText using the replacement pattern
// string result = regex.Replace(InputText,regexReplace);

//// Split the InputText wherever the regex matches
// string[] results = regex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = regex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = regex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = regex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = regex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = regex.GetGroupNumbers();

可能是
\b[aA zZ]{2}[0-9]{5}\b
()。请更好地解释你想用这个正则表达式做什么。字符串可能是一个单词或长文本中的重复项?我建议你用它来解释正则表达式的所有部分。它将向你显示
[aA zZ]
将匹配
a
、介于
a
z
之间的任何内容,或者只匹配
z
…正如其他人所述,这是您的表达式的主要问题可能
\b[aA zZ]{2}[0-9]{5}\b
()。请更好地解释您想对该正则表达式执行的操作。字符串的可能重复项是一个单词或是在一个长文本中?我建议您使用它来解释正则表达式的所有部分。它将向您显示
[aA zZ]
将匹配
a
、介于
a
z
之间的任何内容,或者只匹配
z
…正如其他人所述,这是您的表达式的主要问题可能
\b[aA zZ]{2}[0-9]{5}\b
()。请更好地解释您想对该正则表达式执行的操作。字符串的可能重复项是一个单词或是在一个长文本中?我建议您使用它来解释正则表达式的所有部分。它将向您显示
[aA zZ]
将匹配
a
、介于
a
z
之间的任何内容,或者只匹配
z
…正如其他人所述,这是您的表达式的主要问题可能
\b[aA zZ]{2}[0-9]{5}\b
()。请更好地解释您想对该正则表达式执行的操作。字符串的可能重复项是一个单词或是在一个长文本中?我建议您使用它来解释正则表达式的所有部分。它将向您显示
[aA zZ]
将匹配
a
、介于
a
z
之间的任何内容,或者只匹配
z
……正如其他人所说,这是您表达式的主要问题。这几乎就是我正在寻找的。问题是,对于非常少的记录,我有如下字符串:.AB12345_1、@@AB12345/WS123456789、@@AB12345_11、_AB12345_11.我想从这些字符串中提取AB12345。我应该添加什么来提取我想要的内容(AB12345)?注意:AB12345只是一个示例。通常有两个字母和五个数字。这几乎就是我要查找的。问题是,对于很少数量的记录,我有如下字符串:.AB12345_1、@@AB12345/WS123456789、@@AB12345_11、_AB12345_11。我要从这些字符串中提取AB12345。我应该添加什么来提取我想要的内容(AB12345)?注意:AB12345只是一个示例。通常有两个字母和五个数字。这几乎就是我要查找的。问题是,对于很少数量的记录,我有如下字符串:.AB12345_1、@@AB12345/WS123456789、@@AB12345_11、_AB12345_11。我要从这些字符串中提取AB12345。我应该添加什么来提取我想要的内容(AB12345)?注意:AB12345只是一个例子。通常有两个字母