c# 找到一个字符串中所有形同 xxxdll 的子字符串xxx为任意内容但xxx中有可能包含字符
你可以使用正则表达式来实现这个功能。以下是一个示例代码:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string input = "xxx.dll, abc.dll, 123.dll, xxx.yyy.dll, aaa.bbb.ccc.dll";
string pattern = @"\b\w+(\.\w+)*\.dll\b";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
}
}
上面的代码使用正则表达式 \b\w+(\.\w+)*\.dll\b 来匹配字符串中的 xxx.dll 形式的子字符串。
\b表示匹配单词边界,确保 "xxx.dll" 前后没有其他字符。\w+表示匹配一个或多个字母、数字或下划线。(\.\w+)*表示匹配一个或多个以点号开头的字母、数字或下划线组成的子字符串。\.dll表示匹配以 ".dll" 结尾的字符串。\b表示匹配单词边界,确保 "xxx.dll" 后面没有其他字符。
运行上面的代码,输出结果为:
xxx.dll
abc.dll
123.dll
aaa.bbb.ccc.dll
这些都是在字符串中找到的形同 "xxx.dll" 的子字符串
原文地址: https://www.cveoy.top/t/topic/iiYW 著作权归作者所有。请勿转载和采集!