我正在努力使用正则表达式查找单引号中的文本组。我需要您如何获得建议。
输入文本:
Transfer assignee from 'David, Patricia' to ''
Transfer group from 'Science Department - Support Group' to 'Science Department - Human Resources '
要求:我需要从单引号中提取组和组到并将其保存在数据库中。
我尝试过,但总是得到空结果。这是代码
public void ExtractGroup(string text)
{
var match = Regex.Match(text, @" group from '([^']*)" to '([^']*)", RegexOptions.IgnoreCase);
if(match.Success) {
var groupfrom = match.Groups[1].Value;
var groupTo = match.Groups[2].Value;
}
}
我尝试了您的代码,发现它运行良好:
public static void ExtractGroup()
{
string input = "Transfer assignee from 'David, Patricia' to '' Transfer group from 'Science Department - Support Group' to 'Science Department - Human Resources '";
var match = Regex.Match(input, @" group from '(?<from>[^']*)' to '(?<to>[^']*)'", RegexOptions.IgnoreCase);
if (match.Success)
{
Console.WriteLine(match.Groups["from"]); // Science Department - Support Group
Console.WriteLine(match.Groups["to"]); // Science Department - Human Resources
}
}
但是,我在C#控制台应用程序中运行它。但我认为这不会有任何影响。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句