找到最长的子串正则表达式?

Tom*_*ski 5 c# regex algorithm

有人知道如何使用MatchCollection找到由字母组成的最长子字符串.

public static Regex pattern2 = new Regex("[a-zA-Z]");
public static string zad3 = "ala123alama234ijeszczepsa";
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nko 5

你可以遍历所有比赛并获得最长的比赛:

string max = "";

foreach (Match match in Regex.Matches(zad3, "[a-zA-Z]+"))
    if (max.Length < match.Value.Length)
        max = match.Value;
Run Code Online (Sandbox Code Playgroud)

  • 在foreach循环中显式,`foreach(匹配匹配...) (2认同)