Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Regex.Replace():获取值_C#_Regex_Replace - Fatal编程技术网

C#Regex.Replace():获取值

C#Regex.Replace():获取值,c#,regex,replace,C#,Regex,Replace,我正在分析BB代码图像标记: [img]http://imagesource.com[/img] 我正在使用以下Replace()函数: Regex.Replace(msg, @"\[img\]([^\]]+)\[\/img\]", @"<img src=""$1"" border=""0"" />", RegexOptions.IgnoreCase); Regex.Replace(msg,@“\[img\]”([^\]+)\[\/img\]”,@“”,RegexOptions.I

我正在分析BB代码图像标记:

[img]http://imagesource.com[/img]

我正在使用以下Replace()函数:

Regex.Replace(msg, @"\[img\]([^\]]+)\[\/img\]", @"<img src=""$1"" border=""0"" />", RegexOptions.IgnoreCase);
Regex.Replace(msg,@“\[img\]”([^\]+)\[\/img\]”,@“”,RegexOptions.IgnoreCase);

我需要在解析时获取URL。我需要知道“$1”的价值。可能吗?Regex类以某种方式将“$1”字符串替换为我需要的值,因此必须有一种方法来获取它。

要保留捕获的字符串,只需“捕获”返回值

 string s = Regex.Replace(msg, @"\[img\]([^\]]+)\[\/img\]", @"<img src=""$1"" border=""0"" />", RegexOptions.IgnoreCase); 
string s=Regex.Replace(msg,@“\[img\]”([^\]+)\[\/img\]”,@“,”,RegexOptions.IgnoreCase);

捕获组在正则表达式匹配的Captures属性中可用,如果您进行匹配而不是替换,那么您将有权访问该组。

听起来好像您正在寻找带有接受MatchEvaluator的重载的
替换
方法。可以找到该方法的MSDN页面

请尝试以下方法:

string input = "[img]http://imagesource.com[/img]";
string pattern = @"\[img]([^\]]+)\[\/img]";
string result = Regex.Replace(input, pattern, m =>
    {
        var url = m.Groups[1].Value;
        // do something with url here
        // return the replace value
        return @"<img src=""" + url + @""" border=""0"" />";
     },
    RegexOptions.IgnoreCase);
string result = Regex.Replace(input, pattern,
    m => @"<img src=""" + m.Groups[1].Value + @""" border=""0"" />",
    RegexOptions.IgnoreCase);
string input=“[img]http://imagesource.com[/img]“;
字符串模式=@“\[img]([^\]]+)\[\/img]”;
字符串结果=Regex.Replace(输入,模式,m=>
{
var url=m.Groups[1]。值;
//在这里使用url做一些事情
//返回替换值
返回@”;
},
RegexOptions.IgnoreCase);
这使用了多语句lambda来简化与组的工作,并在返回替换值之前执行更多逻辑。当然,你可以这样做:

string input = "[img]http://imagesource.com[/img]";
string pattern = @"\[img]([^\]]+)\[\/img]";
string result = Regex.Replace(input, pattern, m =>
    {
        var url = m.Groups[1].Value;
        // do something with url here
        // return the replace value
        return @"<img src=""" + url + @""" border=""0"" />";
     },
    RegexOptions.IgnoreCase);
string result = Regex.Replace(input, pattern,
    m => @"<img src=""" + m.Groups[1].Value + @""" border=""0"" />",
    RegexOptions.IgnoreCase);
string result=Regex.Replace(输入、模式、,
m=>@“,
RegexOptions.IgnoreCase);
在上述情况下,不需要返回,但它只返回原始字符串,不需要额外计算。你可以粘贴一些三元运算符并添加逻辑,但看起来会很混乱。多语句lambda要干净得多。你可以考虑用它自己的方法来打破它,如前面提到的MSDN链接,如果它太大或者将被重用在其他<代码>正则表达式中。替换< /代码>努力。


顺便说一句,我还通过删除
]
的转义,稍微简化了您的模式。只有开头的
[
需要转义。

使用正则表达式解析bbcode与使用正则表达式解析HTML具有相同的缺点,因为两者都不是常规语言。请参阅您应该研究使用bbcode解析器(例如,快速谷歌搜索)太棒了!这正是我需要的。谢谢!看起来第一组包含了整个字符串,这就是为什么他使用m.Groups[1]@reggaeguitar正确。索引0处的组包含整个匹配项。抱歉,我列出了该属性。Captures属性包含所有捕获组,组是命名的捕获组。如果没有任何命名组,则组[0]是最后一个匹配的捕获。