Reputation: 45242
Using powershell I want to replace a specific numeric part of a string with another number.
It's best to describe this problem with an example.
$s = ' First part: 65 Second Part'
###################################################################
# this generates " First part: sixty-six Second Part" #
###################################################################
$s -replace '^(\s*First part:\s+)[0-9]+(.*)$', '$1sixty-six$2'
###################################################################
# But I want to generate" First part: 66 Second Part" #
###################################################################
$s -replace '^(\s*First part:\s+)[0-9]+(.*)$', '$166$2'
It doesn't work.
How do I specify "$1 followed by the number 66" as opposed to "$166"?
Upvotes: 2
Views: 441
Reputation: 301147
You can do this:
$s -replace '^(\s*First part:\s+)[0-9]+(.*)$', '${1}66$2'
or
$s -replace '^(?<first>\s*First part:\s+)[0-9]+(.*)$', '${first}66$1'
Both are similar, except that the second one uses a named group.
I am not sure what you are trying to do ( the example seems cooked up) but there must be easier way to do what you want and also you can make use of
$`
and
$'
to signify the portions before and after a match respectively.
Upvotes: 2
Reputation: 52577
Use this syntax: ${1}66$2
Output:
' First part: 66 Second Part'
Upvotes: 5