如何执行Regex.Replace时使用的命名组()

编程入门 行业动态 更新时间:2024-10-26 08:28:26
本文介绍了如何执行Regex.Replace时使用的命名组()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何执行Regex.Replace当我使用一个名为捕获?到目前为止,我已经得到了这一点,它不会是我想要的东西,但不是在路上,我希望它:

How do I use named captures when performing Regex.Replace? I have gotten this far and it does what I want but not in the way I want it:

[TestCase("First Second", "Second First")] public void NumberedReplaceTest(string input, string expected) { Regex regex = new Regex("(?<firstMatch>First) (?<secondMatch>Second)"); Assert.IsTrue(regex.IsMatch(input)); string replace = regex.Replace(input, "$2 $1"); Assert.AreEqual(expected, replace); }

我想这两个词与名为捕获匹配,然后使用(命名)进行更换时捕获。

I want to match the two words with named captures and then use the (named) captures when performing the replace.

推荐答案

而不是$ 2 $ 1,您可以用$ {secondMatch} $ {firstMatch}。

Instead of "$2 $1", you can use "${secondMatch} ${firstMatch}".

有是所有的列表在更换你可以在这里做 。

There is a list of all the replacements you can do here.

下面是一个简短的列表:

Here is an abbreviated list:

$号 - 捕获的组由数

$number - The captured group by number.

$ {name}的 - 由名被俘组

$ - $文字。

$$ - $ literal.

$&放大器; - 整场比赛

$` - 在比赛前输入字符串

$` - The input string before the match.

$ - 比赛结束后输入字符串

$' - The input string after the match.

$ + - 拍摄的最后一组

$+ - The last group captured.

$ _ - 整个输入字符串。

$_ - The entire input string.

更多推荐

如何执行Regex.Replace时使用的命名组()

本文发布于:2023-11-04 00:43:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Regex   Replace

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!