C# Get line of multiline String starting with specific word

I have a multiline string, say

abcde   my first line
fghij   my second line
klmno   my third line

All of this is one String, but what I want to do now is to get the content (substring) of this string which is starting with a specific word, for example “fghij”. So if I do a method and pass “fghij” to it, it should return me “fghij my second line” in that case.

The following I tried, but it does not work, unfortunately m.Success is always false:

String getLineBySubstring(String myInput, String mySubstring)
    {
        Match m = Regex.Match(myInput, "^(" + mySubstring + "*)", RegexOptions.Multiline);
        Console.WriteLine("getLineBySubstring operation: " + m.Success);
        if (m.Success == true)
        {
            return m.Groups[0].Value;
        }
        else
        {
            return "NaN";
        }
    }


Source: regex

Leave a Reply