How to find words in a string that start with specific character in C#

3 Answers

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

class Program
{
    static void Main() {
        string s = "php csharp java cpp python cobol swift";  

        foreach (Match match in Regex.Matches(s, @"(?<!\w)c\w+")) {
            Console.WriteLine(match.Value);
        }
    }
}




/*
run:

csharp
cpp
cobol

*/

 



answered Feb 27, 2021 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main() {
        string s = "c php csharp java cpp python cobol swift";  

        string regularExpression = @"\bc\w*";
        MatchCollection mc = Regex.Matches(s, regularExpression);
        
        foreach (Match match in mc) {
            Console.WriteLine(match);
        }
    }
}




/*
run:

c
csharp
cpp
cobol

*/

 



answered Feb 27, 2021 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;
 
class Program
{
    static void Main() {
        string s = "C php Csharp java cpp python cobol swift";  
 
        string pattern = @"\b[c]\w*";  
        
        Regex rg = new Regex(pattern, RegexOptions.IgnoreCase);  
      
        MatchCollection mc = rg.Matches(s);  
        
        foreach (Match match in mc) {
            Console.WriteLine(match);
        }
    }
}
 
 
 
 
/*
run:
 
C
Csharp
cpp
cobol
 
*/

 



answered Feb 27, 2021 by avibootz
...