C# 正则表达式查找字符串中所有 "xxx.dll" 格式的子字符串
使用正则表达式来找到字符串中所有形同 "xxx.dll" 的子字符串。\n\n以下是一个示例代码:\n\ncsharp\nusing System;\nusing System.Text.RegularExpressions;\n\npublic class Program\n{\n public static void Main(string[] args)\n {\n string input = \"This is a test string with abc.dll, def.dll, and xyz.dll.\";\n\n // 使用正则表达式匹配字符串中的 \"xxx.dll\" 格式\n Regex regex = new Regex(@\"\w+\.dll\");\n MatchCollection matches = regex.Matches(input);\n\n // 输出匹配到的子字符串\n foreach (Match match in matches)\n {\n Console.WriteLine(match.Value);\n }\n }\n}\n\n\n上述代码中,我们使用了正则表达式 \w+\.dll 来匹配字符串中的 "xxx.dll" 格式,其中 \w+ 表示匹配一个或多个字母、数字或下划线,\. 表示匹配一个点号。然后,我们使用 MatchCollection 对象来存储所有匹配到的子字符串,并通过遍历输出每个匹配到的子字符串。\n\n运行上述代码,输出结果为:\n\nabc.dll\ndef.dll\nxyz.dll\n\n\n注意:上述代码只能找到第一个匹配的子字符串,如果要找到所有匹配的子字符串,可以使用 Regex.Matches 方法来获取一个 MatchCollection 对象,然后遍历输出每个匹配到的子字符串。
原文地址: https://www.cveoy.top/t/topic/p077 著作权归作者所有。请勿转载和采集!