如何执行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时使用的命名组()
发布评论