有没有一种方法可以搜索一个解决方案中的所有数字文字?例如,这些将包括:
Margin = new Thickness(10); int i = 10;
而这些不是:
Margin = margin123; int i100 = j100;
jei2mxaa1#
尝试启用match whole word选项沿着regex (\d)*一起
match whole word
(\d)*
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
2条答案
按热度按时间jei2mxaa1#
尝试启用
match whole word
选项沿着regex(\d)*
一起kknvjkwl2#
不是那么有效的解决方案,但可能也在工作:
输出: