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

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[0]); Console.ReadKey(); } } }

 

posted @ 2013-12-13 13:08  爹子王  阅读(2018)  评论(0编辑  收藏  举报