[英]Regex match first two occurrences of a capital letter followed by several lower case
我一直在通過此處的示例查看如何進行類似的正則表達式匹配,但是我無法使其適合我的情況。
我有一個類似ThisisMystringItsTooLong
的字符串,我想找回ThiMys
( 前兩個大寫字母,然后是兩個小寫字母 ),但是,如果該字符串只是Thisismystring
(只有一個大寫字母),那么我只是想
退Thi
我嘗試過([AZ]{1})([az]{2}){0,1}
僅在匹配的大寫字母超過2個的情況下才第一次出現我的匹配項,但是我我不確定如何應用第二個條件。
您不能僅使用正則表達式來做到這一點,因為匹配始終是輸入的連續子字符串。 您當然可以將多個匹配項組合為一個最終結果。
String.Join(String.Empty, Regex.Matches(input, "[A-Z][a-z]{2}")
.Cast<Match>()
.Take(2)
.Select(match => match.Value));
您可以創建這樣的方法:
public string GetMyCharacters(string s)
{
int numOfCaps = Regex.Matches(s, "[A-Z]").Count;
if (numOfCaps > 2)
{
var matches = Regex.Matches(s, "[A-Z][a-z]{2}");
return matches[0].Value + matches[1].Value;
}
else if (numOfCaps == 1)
{
var matches = Regex.Matches(s, "[A-Z][a-z]{2}");
return matches[0].Value;
}
else { return null; }
}
然后這樣稱呼它:
Console.WriteLine(GetMyCharacters("ThisisMystringItsTooLong")); // ThiMys
Console.WriteLine(GetMyCharacters("Thisismystring")); // Thi
Console.WriteLine(GetMyCharacters("wijfowro"));// null
我將只使用Regex模式[AZ][az]{2}
並“手動”執行其他邏輯。
public string ShortIdentifier(string longIdentifier)
{
MatchCollection matches = Regex.Matches(longIdentifier, "[A-Z][a-z]{2}");
if (matches.Count == 1) {
return matches[0].Value;
} else if (matches.Count >= 2) {
return matches[0].Value + matches[1].Value;
}
return longIdentifier.Substring(0, Math.Min(longIdentifier.Length, 6));
// Or return whatever you want when there is no match.
}
如果要返回一個大寫字母,然后返回一個或兩個小寫字母,請將正則表達式更改為[AZ][az]{1,2}
。
我最初誤解了需求,但是這里是固定版本:
Regex.Replace(
"ThisisMystringItsTooLong",
"^(?:.*?([A-Z][a-z]{2}))?(?:.*?([A-Z][a-z]{2}))?.*$",
"$1$2"
)
它與整個輸入字符串匹配,從開始(^)到結束($),它將分為以下部分:
(?:.*?([A-Z][a-z]{2}))? - optional non-capturing group, which consists of
a bunch of non-greedy anything followed
by substring sought, which is captured
(?:.*?([A-Z][a-z]{2}))? - another exactly same group; if we want to place
some limits on what can be between substrings
sought (like no spaces etc.) it goes here
instead of the anything
?.* - anything else
然后,它通過使用Regex.Replace方法將兩個匹配(可能為空)匹配來構造輸出字符串。 經過測試:
"ThisisMystringItsTooLong" -> "ThiMys"
"Thisismystring" -> "Thi"
"thisismystring" -> ""
"that is His String" -> "HisStr"
"oh Hi There!" -> "The"
"oh Hi There Go Here" -> "TheHer"
與Danies的答案不同,它除了使用正則表達式外沒有使用其他任何東西,但是不確定它的性能是好是壞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.