C# 正则匹配键盘行(或列)连续字符 ,3连连续字符匹配

https://blog.csdn.net/qq_20173195/article/details/126989662 参考
using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(CheckKeyboardContinuousChar(",./"));
    }

    public static bool CheckKeyboardContinuousChar(string str)
    {
        char[][] c1 = {
            new char[] { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+' },
            new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '{', '}', '|' },
            new char[] { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ':', '"' },
            new char[] { 'z', 'x', 'c', 'v', 'b', 'n', 'm', '<', '>', '?' }
        };
        char[][] c2 = {
            new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=' },
            new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\' },
            new char[] { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'' },
            new char[] { 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/' }
        };

        str = str.ToLower();
        char[] strArray = str.ToCharArray();
        int[] y = new int[strArray.Length];
        int[] x = new int[strArray.Length];

        for (int c = 0; c < strArray.Length; c++)
        {
            y[c] = 0; // 当做~`键处理
            x[c] = -1;
            for (int i = 0; i < c1.Length; i++)
            {
                for (int j = 0; j < c1[i].Length; j++)
                {
                    if (strArray[c] == c1[i][j])
                    {
                        y[c] = i;
                        x[c] = j;
                    }
                }
            }

            if (x[c] != -1)
            {
                continue;
            }

            for (int i = 0; i < c2.Length; i++)
            {
                for (int j = 0; j < c2[i].Length; j++)
                {
                    if (strArray[c] == c2[i][j])
                    {
                        y[c] = i;
                        x[c] = j;
                    }
                }
            }
        }

        for (int c = 1; c < strArray.Length - 1; c++)
        {
            if (y[c - 1] == y[c] && y[c] == y[c + 1]) // 横着同一行
            {
                if ((x[c - 1] + 1 == x[c] && x[c] + 1 == x[c + 1]) || (x[c + 1] + 1 == x[c] && x[c] + 1 == x[c - 1]))
                {
                    return true;
                }
            }
            if (x[c - 1] == x[c] && x[c] == x[c + 1]) // 竖着同一列
            {
                if ((y[c - 1] + 1 == y[c] && y[c] + 1 == y[c + 1]) || (y[c + 1] + 1 == y[c] && y[c] + 1 == y[c - 1]))
                {
                    return true;
                }
            }
            if ((x[c - 1] + 1 == x[c] && x[c] + 1 == x[c + 1]) || (x[c - 1] - 1 == x[c] && x[c] - 1 == x[c + 1]))
            {
                if ((y[c - 1] + 1 == y[c] && y[c] + 1 == y[c + 1]) || (y[c + 1] + 1 == y[c] && y[c] + 1 == y[c - 1]))
                {
                    return true;
                }
            }
        }

        return false;
    }
}

 

posted @ 2024-05-21 15:59  LuoCore  阅读(2)  评论(0编辑  收藏  举报