Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a -replace in powershell where a number follows a sequence

For example, let's say I want to take the string Hello World and use a -replace to output "World1". This was my attempt at the expression:

"Hello World" -replace '.*(World)','$11'

But the problem here is that it's seeing $11 as the 11th sequence, not $1 followed by 1. I've tried searching for an escape character to specify that I want a 1 following the $1, but haven't found anything.

The real world problem I was trying to solve is to take a bunch of email addresses and create aliases with a number on the end. For example

[email protected]

becomes

[email protected]
[email protected]
like image 729
eltommo Avatar asked Dec 05 '25 05:12

eltommo


1 Answers

Try "Hello World" -replace '.*(World)','${1}1'

It seemed to work here

        var s1 = "Hello World";
        var r = ".*(World)";
        var p = "${1}1";
        var outstr = Regex.Replace(s1, r, p);
        Console.WriteLine(outstr);

Output World1

like image 169
mrwaim Avatar answered Dec 07 '25 21:12

mrwaim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!