如何将匹配结果列表从正则表达式转换为List<string>?我有这个功能,但它总是会产生异常,
无法将类型为"System.Text.RegularExpressions.Match"的对象强制转换为"System.Text.RegularExpressions.CaptureCollection".
public static List<string> ExtractMatch(string content, string pattern)
{
List<string> _returnValue = new List<string>();
Match _matchList = Regex.Match(content, pattern);
while (_matchList.Success)
{
foreach (Group _group in _matchList.Groups)
{
foreach (CaptureCollection _captures in _group.Captures) // error
{
foreach (Capture _cap in _captures)
{
_returnValue.Add(_cap.ToString());
}
}
}
}
return _returnValue;
}
Run Code Online (Sandbox Code Playgroud)
如果我有这个字符串,
I have a dog and a cat.
Run Code Online (Sandbox Code Playgroud)
正则表达式
dog|cat
Run Code Online (Sandbox Code Playgroud)
我希望函数将结果返回到 List<string>
dog
cat
Run Code Online (Sandbox Code Playgroud) 我在Snow Leopard中安装了Mono 2.6.7并想运行LINQPad.我已经启动LINQPad(v2.21)但是立即得到一个FileNotFoundException.有没有人能够成功运行它?
我认为异常是因为它试图读取/写入配置文件或其他东西,但希望有一些解决方法.
谢谢.
编辑:使用"Olive"构建Mono(WPF所需):
在终端:
svn co svn://anonsvn.mono-project.com/source/trunk/olive
cd /Users/(your user name)/olive
./configure --prefix=/Users/(your user name)/olive --with-glib=embedded
make
make install
Run Code Online (Sandbox Code Playgroud)
现在在Finder中导航到:
/ Users /(您的用户名)/ olive/lib/mono/gac
将这些文件夹(例如:PresentationCore,PresentationFramework)复制到:
/Library/Frameworks/Mono.framework/Versions/2.6.7/lib/mono/gac(当前的Mono版本是2.6.7,但这显然可能有所不同)
编辑:不幸的是,现在我在运行LINQPad时得到了这个:
警告**:无法加载类System.Windows.Resources.AssemblyAssociatedContentFileAttribute,在LINQPad中使用不能加载,在LINQPad中使用
编辑: Xamarin工作簿最近发布了1.0(https://developer.xamarin.com/workbooks/),是我在macOS上看到的LINQPad最接近的.
编辑(2017年9月): 这仍然是投机性的!
使用Docker和Windows子系统Linux(WSL)可以运行大多数Windows应用程序(包括GUI应用程序):

有关详细演练,请参阅https://blog.jessfraz.com/post/windows-for-linux-nerds/上的博客文章.
我开始写一本C#书,我决定把RegEx放到混音中,让枯燥的控制台练习更有趣.我想要做的是在控制台中询问用户他们的电话号码,根据RegEx进行检查,然后捕获数字,这样我就可以按照我想要的方式对其进行格式化.除了RegEx捕获部分之外,我已经完成了所有工作.如何将捕获值转换为C#变量?
也可以随意更正任何代码格式或变量命名问题.
static void askPhoneNumber()
{
String pattern = @"[(]?(\d{3})[)]?[ -.]?(\d{3})[ -.]?(\d{4})";
System.Console.WriteLine("What is your phone number?");
String phoneNumber = Console.ReadLine();
while (!Regex.IsMatch(phoneNumber, pattern))
{
Console.WriteLine("Bad Input");
phoneNumber = Console.ReadLine();
}
Match match = Regex.Match(phoneNumber, pattern);
Capture capture = match.Groups.Captures;
System.Console.WriteLine(capture[1].Value + "-" + capture[2].Value + "-" + capture[3].Value);
}
Run Code Online (Sandbox Code Playgroud)