C#查找以某个字母开头另一字母结尾的字符串

时间:2022-05-24 20:23:30
using System;
using System.Text.RegularExpressions; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string oldStr = Console.ReadLine();
string pattern = @"\Ba\S*c\B";
// \B表示不是字边界的位置,这个串表示以a开头以c结尾的任意字符串。如hmabbccln中的abbcc这个串
// \b表示字边界,就是以a开头以c结尾的单词。如I know abc中的abc这个单词
// \S表示任何不是空白的字符。\s表示任何空白字符。
MatchCollection match = Regex.Matches(oldStr, pattern, RegexOptions.IgnorePatternWhitespace |
          RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
Console.WriteLine(match[]);
Console.ReadKey(); }
}
}