How to match words that start with S in a string with C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    public static void WordsStartWith(string str, string expr) {
        MatchCollection mc = Regex.Matches(str, expr);

        foreach (var word in mc)
            Console.WriteLine(word);
    }
    static void Main() {
        string str = "Stack C# sold net Safe c programming s saber S";

        WordsStartWith(str, @"\bS\S*");
    }
}



/*
run:

Stack
Safe
S

*/

 



answered Oct 27, 2022 by avibootz

Related questions

...