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("");
}
}
}
C# 提取连续字符
最新推荐文章于 2022-09-12 15:27:26 发布