Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the longest substring regex?

Someone knows how to find the longest substring composed of letters using using MatchCollection.

public static Regex pattern2 = new Regex("[a-zA-Z]");
public static string zad3 = "ala123alama234ijeszczepsa";
like image 289
Karol Pisarzewski Avatar asked Jan 02 '18 13:01

Karol Pisarzewski


People also ask

How do I find the largest substring?

The longest common substrings of a set of strings can be found by building a generalized suffix tree for the strings, and then finding the deepest internal nodes which have leaf nodes from all the strings in the subtree below it.

How do you find the length of a substring?

There are two ways to find the length of the longest substring: The Simple method extracts all the substrings from a string, and then calculates the length of substrings with only distinct characters. There will be [(n * (n + 1)) / 2] substrings in a string of n characters.

How do you find longest substring without repeating characters in a string?

Run a loop from i = 0 till N – 1 and consider a visited array. Run a nested loop from j = i + 1 to N – 1 and check whether the current character S[j] has already been visited. If true, break from the loop and consider the next window. Else, mark the current character as visited and maximize the length as j – i + 1.


4 Answers

You can loop over all matches and get the longest:

string max = "";

foreach (Match match in Regex.Matches(zad3, "[a-zA-Z]+"))
    if (max.Length < match.Value.Length)
        max = match.Value;
like image 137
Dmitry Bychenko Avatar answered Oct 09 '22 15:10

Dmitry Bychenko


Try this:

 MatchCollection matches = pattern2.Matches(txt);
 List<string> strLst = new List<string>();
 foreach (Match match in matches)
     strLst.Add(match.Value);
 var maxStr1 = strLst.OrderByDescending(s => s.Length).First();

or better way :

 var maxStr2 = matches.Cast<Match>().Select(m => m.Value).ToArray().OrderByDescending(s => s.Length).First();
like image 33
Aria Avatar answered Oct 09 '22 17:10

Aria


best solution for your task is:

string zad3 = "ala123alama234ijeszczepsa54dsfd";
string max = Regex.Split(zad3,@"\d+").Max(x => x);
like image 36
Leon Barkan Avatar answered Oct 09 '22 17:10

Leon Barkan


You must change your Regex pattern to include the repetition operator + so that it matches more than once.

[a-zA-Z] should be [a-zA-Z]+

You can get the longest value using LINQ. Order by the match length descending and then take the first entry. If there are no matches the result is null.

string pattern2 = "[a-zA-Z]+";
string zad3 = "ala123alama234ijeszczepsa";

var matches = Regex.Matches(zad3, pattern2);

string result = matches
                 .Cast<Match>()
                 .OrderByDescending(x => x.Value.Length)
                 .FirstOrDefault()?
                 .Value;

The string named result in this example is:

ijeszczepsa

like image 1
Equalsk Avatar answered Oct 09 '22 16:10

Equalsk