C# 正则表达式匹配首两个大写字母,后跟几个小写字母
我一直在查看这里的示例,了解如何进行类似的正则表达式匹配,但我无法使其适用于我的情况 我有一个字符串类似于C# 正则表达式匹配首两个大写字母,后跟几个小写字母,c#,regex,C#,Regex,我一直在查看这里的示例,了解如何进行类似的正则表达式匹配,但我无法使其适用于我的情况 我有一个字符串类似于thismystringitstoolong,我想返回ThiMys(大写字母的前两次出现,其后是两个小写字母) 但是,如果那里的字符串只是Thisismystring(只有一个大写字母),那么我只想要 返回Thi 我已经尝试了([A-Z]{1})([A-Z]{2}{0,1},在有两个以上大写字母的情况下,仅获取匹配的第一个匹配项,但我不确定如何应用第二个条件。您可以创建如下方法: publi
thismystringitstoolong
,我想返回ThiMys
(大写字母的前两次出现,其后是两个小写字母)
但是,如果那里的字符串只是Thisismystring
(只有一个大写字母),那么我只想要返回
Thi
我已经尝试了
([A-Z]{1})([A-Z]{2}{0,1}
,在有两个以上大写字母的情况下,仅获取匹配的第一个匹配项,但我不确定如何应用第二个条件。您可以创建如下方法:
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
那么就这样称呼它:
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
您可以创建如下方法:
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
那么就这样称呼它:
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
仅使用正则表达式无法做到这一点,因为匹配始终是输入的连续子字符串。当然,您可以将多个匹配组合为一个最终结果
String.Join(String.Empty, Regex.Matches(input, "[A-Z][a-z]{2}")
.Cast<Match>()
.Take(2)
.Select(match => match.Value));
String.Join(String.Empty,Regex.Matches(输入“[A-Z][A-Z]{2}”)
.Cast()
.采取(2)
.Select(match=>match.Value));
您不能仅使用正则表达式执行此操作,因为匹配始终是输入的连续子字符串。当然,您可以将多个匹配组合为一个最终结果
String.Join(String.Empty, Regex.Matches(input, "[A-Z][a-z]{2}")
.Cast<Match>()
.Take(2)
.Select(match => match.Value));
String.Join(String.Empty,Regex.Matches(输入“[A-Z][A-Z]{2}”)
.Cast()
.采取(2)
.Select(match=>match.Value));
试试哪个是
然后,您需要将两个捕获组连接起来,以获得最终结果。试试哪一个是
然后,您需要连接两个捕获组以获得最终结果。我只需使用正则表达式模式
[A-Z][A-Z]{2}
,并“手动”执行其他逻辑
如果要返回一个大写字母,后跟一个或两个小写字母,请将正则表达式更改为
[A-Z][A-Z]{1,2}
。我只需使用正则表达式模式[A-Z][A-Z]{2}
,并“手动”执行其他逻辑
如果要返回一个大写字母,后跟一个或两个小写字母,请将正则表达式更改为
[A-Z][A-Z]{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的回答不同的是,除了正则表达式之外,没有使用任何东西,但不确定它的性能是更好还是更差。我最初误解了这些要求,但下面是固定版本:
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的答案不同的是,除了正则表达式之外,没有使用任何东西,但不确定它的性能是更好还是更差。您不能将第一个匹配项和第二个匹配项连接起来吗?否则,你不能用正则表达式跳过字符。@MattBurland是的,我想我可以这样做,我不太经常使用正则表达式,所以我很好奇是否可以用我提到的方式处理多个场景。不过这是个不错的选择。我不知道你说的跳过角色是什么意思,我更多的是尝试按照substring@JWiley,我为您创建了一个函数,您不能将第一个匹配项和第二个匹配项连接起来吗?否则,你不能用正则表达式跳过字符。@MattBurland是的,我想我可以这样做,我不太经常使用正则表达式,所以我很好奇是否可以用我提到的方式处理多个场景。不过这是个不错的选择。我不知道你说的跳过角色是什么意思,我更多的是尝试按照substring@JWiley,我为您创建了一个函数,您能解释一下后一部分是如何为第二个Join参数工作的吗?它只获取
[a-Z][a-Z]{2}
,,出于技术原因对结果匹配集合进行强制转换(MatchCollection
仅实现IEnumerable
,而不实现IEnumerable
),最多进行前两次匹配,获取每个参数的匹配文本,并最终通过使用空字符串连接它们来连接它们。能否解释后一部分如何用于第二个连接参数?它只获取[A-Z][A-Z]{2}
的所有匹配项,出于技术原因对结果匹配集合进行强制转换(MatchCollection
仅实现IEnumerable
,而不实现IEnumerable
),最多获取前两个匹配项,获取每个匹配项的匹配文本,并最终通过使用空字符串连接它们来连接它们。我正在尝试实现您的解决方案,但我看到它跳过了一个匹配项,如HellothissString 1
。正则表达式只查找一个大写字母和两个小写字母。我想要匹配项返回的是HelThi
,但此方法将返回heloth
。您肯定在某个地方输入了错误。我得到的是“HelThi”
。第一个匹配将是“Hel”
,第二个匹配将是“Thi”
,还有第三个未使用的“Str”
。啊,是的,我添加了一些东西来解释只有一个小写字母的单词,带有{1,2}
,这似乎迫使它匹配准确的长度。有没有办法检查任何数字,但只返回前2个?[a-Z][a-Z]{1,2}
应该有效。它将匹配尽可能多的小写字母,但最多两个,即使单词较长。(已测试)。对于“Heree”,它将返回