Reputation: 115
I am trying to find an undefined series of characters and replace them with a defined set via powershell. The particularly troublesome line of script involves altering the xml manifest of an Android project:
(get-content "AndroidManifest.xml") |
foreach { $_ -replace "@drawable/*_icon", "@drawable/$icon_icon"} |
set-content "AndroidManifest.xml"
The idea of this line is that it retrieves the content, searches and replaces the icon string (replacing the existing icon name with that represented by $icon) and then passes this back into the file.
Unfortunately, when executed this doesn't appear to actually do its job, and the string is just not changing. The existing string is changeable, so no values can be inputted (hence the wildcard).
Can anybody suggest a solution to this problem?
Upvotes: 2
Views: 173
Reputation: 26769
I think the regular expression you're using isn't quite correct. The *
modifier matches zero or more of the previous character, so /*_icon
matches zero or more forward slashes followed by _icon
.
Second, in your replacement string, what you match is going to be replaced with the value of the $icon_icon
variable. You need to wrap the $icon
variable as an expression, $($icon)_icon
or a string format, "@drawable/{0}_icon" -f $icon
. I prefer string formats.
Put it all together and you get:
$_ -replace "@drawable/(.*?)_icon", ("@drawable/{0}_icon" -f $icon)
Upvotes: 3