簡體   English   中英

正則表達式匹配子字符串

[英]Regular expression match substring

我試圖創建一個正則表達式來提取所有匹配的內容:

[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 '%[^az][az][az][0-9][0-9][0-9][0-9][0-9][^0-9]%' or field like '[az][az][0-9][0-9][0-9][0-9][0-9][^0-9]%' THEN fnRegExMatch(field, '[a-zA-Z]{2}[0-9]{5}') ELSE field

首先[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])

(?<![az])是一個否定的后面假設,假設您在兩個必須的字母之前沒有字母
(?![0-9])否定的前瞻 ,假設您在必填的5個數字之后沒有數字

這將是代碼以及用法示例。

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM