我在excel中有一个公式,从C#代码读取时看起来像这样
"=HYPERLINK(CONCATENATE(\"https://abc.efghi.rtyui.com/#/wqeqwq/\",#REF!,\"/asdasd\"), \"View asdas\")"
我想使用正则表达式从此字符串中获取URL,即
https://abc.efghi.rtyui.com/#/wqeqwq/#REF!/asdasd
网址可能不同,但公式的格式将保持不变。
"=HYPERLINK(CONCATENATE(\"{SOME_STRING}\",#REF!,\"{SOME_STRING}\"), \"View asdas\")"
答案 0 :(得分:1)
试试这样:
(?<=HYPERLINK\(CONCATENATE\(")[^"]+
positive lookbehind允许我们从完整匹配中跳过URL的前面部分。
如果您之间有任意数量的空格,请添加一些\s*
,例如请参阅this example,其中还会在字符串的开头显示转义的=
。
示例代码:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?<=HYPERLINK\(CONCATENATE\("")[^""]+";
string input = @"=HYPERLINK(CONCATENATE(""https://abc.efghi.rtyui.com/#/wqeqwq/"",#REF!,""/asdasd""), ""View asdas"")";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
附录:这是另一种使用捕获组和正则表达式替换以提取生成的URL字符串的技术(在CONCATENATE发生之后):
^\=HYPERLINK\(CONCATENATE\("([^"]+)",([^,]+),"([^"]+)".*$
string pattern = @"^\=HYPERLINK\(CONCATENATE\(""([^""]+)"",([^,]+),""([^""]+)"".*$";
string substitution = @"$1$2$3";
string input = @"=HYPERLINK(CONCATENATE(""https://abc.efghi.rtyui.com/#/wqeqwq/"",#REF!,""/asdasd""), ""View asdas"")";
Regex regex = new Regex(pattern);
string result = regex.Replace(input, substitution, 1);
答案 1 :(得分:0)
您可以使用正则表达式中的捕获组从公式中提取URL,如下所示:
string inputString = "=HYPERLINK(CONCATENATE(\"https://abc.efghi.rtyui.com/#/wqeqwq/\",#REF!,\"/asdasd\"), \"View asdas\")";
string regex = "CONCATENATE\\(\"([\\S]+)\",#REF!,\"([\\S]+)\"\\)";
Regex substringRegex = new Regex(regex, RegexOptions.IgnoreCase);
Match substringMatch = substringRegex.Match(inputString);
if (substringMatch.Success)
{
string url = substringMatch.Groups[1].Value + "#REF!" + substringMatch.Groups[2].Value;
}
我在正则表达式中定义了两个捕获组。一个用于在#REF之前提取部分URL!另一个用于在#REF!之后提取部分URL。然后我用#REF连接所有提取的部分!获取最终的URL。