C# 提取连续字符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 提取连续字符
{
    class Program
    {
        static void Main(string[] args)
        {
            init();
            match("abc123abc123456789123465");
            match("123a1bc123abc1234567891234656565");
            Console.ReadKey();
        }

        public static int[,] dfa = new int[128, 128];
        public static void init()
        {
            for (int i = '0'; i < '9'; ++i)
            {
                dfa[i, i + 1] = i + 1;
                dfa[0, i] = i;
            }
        }
        public static void match(String str)
        {
            int state = 0;
            int last = 0;
            for (int i = 0; ; ++i)
            {
                if (i == str.Length)
                {
                    if (i > last)
                    {
                        for (int j = last; j < i; ++j) Console.Write("{0}", str[j]);
                        Console.WriteLine("");
                    }
                    break;
                }
                char c = str[i];
                if (dfa[state, c] == 0)
                {
                    if (i > last)
                    {
                        for (int j = last; j < i; ++j) Console.Write("{0}", str[j]);
                        Console.WriteLine("");
                    }
                    if (c >= '0' && c <= '9')
                    {
                        state = c; last = i;
                    }
                    else
                    {
                        state = 0; last = i + 1;
                    }
                }
                else
                {
                    state = dfa[state, c];
                }
            }
            Console.WriteLine("");
        }

    }
}


版权声明:本文为wpyok168原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。