我尝试创建一个正则表达式来提取匹配的所有内容:
[aA-zZ]{2}[0-9]{5}
问题是我想要在匹配时排除,例如。 ABCD12345678
任何人都可以帮我解决这个问题吗?
EDIT1: 我在字符串中查找两个字母和五个数字,但是当我有像ABCD12345678这样的字符串时,我想从匹配中排除,因为当我使用上面的正则表达式时,它将返回CD12345。
EDIT2: 我没有检查所有内容,但我认为我找到了答案:
WHEN field is null then field
WHEN fnRegExMatch(field, '[a-zA-Z]{2}[0-9]{5}') = 'N/A' THEN field
WHEN field like '%[^a-z][a-z][a-z][0-9][0-9][0-9][0-9][0-9][^0-9]%' or field like '[a-z][a-z][0-9][0-9][0-9][0-9][0-9][^0-9]%' THEN fnRegExMatch(field, '[a-zA-Z]{2}[0-9]{5}')
ELSE field
答案 0 :(得分:2)
首先[aA-zZ]
没有任何意义,第二个使用单词边界:
\b[a-zA-Z]{2}[0-9]{5}\b
您还可以使用不区分大小写的修饰符:
(?i)\b[a-z]{2}[0-9]{5}\b
根据你的评论,似乎你可能在五位数之后加下划线。在这种情况下,单词边界不起作用,你必须改为:
(?i)(?<![a-z])([a-z]{2}[0-9]{5})(?![0-9])
(?<![a-z])
是一个negative lookbehind,假设您在两个强制要求之前没有来信
(?![0-9])
是一个negative lookahead,假设您在五个必须之后没有数字
答案 1 :(得分:1)
这将是代码,以及使用示例。
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();