本篇内容主要讲解“C#怎么匹配以S开头的单词”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#怎么匹配以S开头的单词”吧!
下面的实例匹配了以 'S' 开头的单词:
实例
using System;
using System.Text.RegularExpressions;
namespace RegExApplication
{
class Program
{
private static void showMatch(string text, string expr)
{
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc)
{
Console.WriteLine(m);
}
}
static void Main(string[] args)
{
string str = "A Thousand Splendid Suns";
Console.WriteLine("Matching words that start with 'S': ");
showMatch(str, @"\bS\S*");
Console.ReadKey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Matching words that start with 'S':
The Expression: \bS\S*
Splendid
Suns
到此,相信大家对“C#怎么匹配以S开头的单词”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!