Visual Studio 查找解决方案中的所有数字文字

x4shl7ld  于 2023-05-01  发布在  其他
关注(0)|答案(2)|浏览(134)

有没有一种方法可以搜索一个解决方案中的所有数字文字?
例如,这些将包括:

Margin = new Thickness(10);
int i = 10;

而这些不是:

Margin = margin123;
int i100 = j100;
jei2mxaa

jei2mxaa1#

尝试启用match whole word选项沿着regex (\d)*一起

kknvjkwl

kknvjkwl2#

不是那么有效的解决方案,但可能也在工作:

using System;
using System.Linq;
using System.Collections.Generic;

namespace Demo
{
    class Program
    {
        static readonly char[] numbers = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        static readonly char[] allowedChars = new char[] {'(', ')', '{', '}', '[', ']', ' '};
        public static void Main(string[] args)
        {
            const string input = @"
Margin = new Thickness(10);
int i = 10;
Margin = margin123;
int i100 = j100;
a10
124b
";
            List<string> matches = new List<string>();
            foreach (var row in input.Split('\n')) 
            {
                int start = row.IndexOfAny(numbers);
                
                if (start == -1) 
                    continue;
                
                if (start == 0 || numbers.Contains(row[start - 1]) || allowedChars.Contains(row[start - 1]))
                {
                    matches.Add(row);
                } 
            }
            
            Console.WriteLine(string.Join("\n\r", matches));
            Console.ReadKey(true);
        }
    }
}

输出:

Margin = new Thickness(10);
int i = 10;
124b

相关问题