Question about -replace
- I'm coming into powershell from a heavy perl background so naturally my first place of interest is to understand the implementation of Regular Expressions. I see a difference in how replace works compared to how perl does it by default.'test' -replace 't', 'k'returns the equivalent of perl's s/t/k/g instead of s/t/k/it's returning kesk and I want it to return kestHow do I ensure that -replace will not do a global replace and only replace the first instance of the match? I can't seem to find any msdn documentation on this either so any links would be appreciated. I'm also not sure how to search within powershell to find documentation for this function/module/ (any help getting my lingo down is appreciated). help replace doesn't give me anything.
Answers
I'm not sure which is more obnoxious... telling me to google or having me read article after article that doesn't apply to what I'm asking for help with. If you don't like my question please don't answer. If I misunderstand the intention of your links please quote the part that shows the answer.
About "-replace":I'm not asking for general information about regular expressions or how to form a regex. I'm asking how you can use -replace without a global modifier. Alternatively, I also asked how to get the help page for -replace.... as mentioned I don't even know what to call an operator that uses this -functionname.... I'm having trouble finding any kind of documentation for it.
PS>get-help about_Comparison_Operators
As Karl mentions, this is free support. We are trying to help. We come across all kinds of people looking for support, and most aren't able to do a simple search to try to look for into on their own. I'll also try to help by just providing links, because I'm very busy and hope that sometimes just providing the right search term might send someone off on the right track (if there were previously completely lost).
So thanks Karl for trying to help anyone who comes here... I really appreciate it.- Marked As Answer byTome Tanasovski Saturday, November 07, 2009 12:11 AM
All Replies
- Proposed As Answer byMarco ShawMVP, ModeratorFriday, November 06, 2009 8:02 PM
- Unproposed As Answer byTome Tanasovski Friday, November 06, 2009 8:52 PM
- I had already read that article, but I do not see anything about doing what I want to do. Am I missing something? Is it listed there somewhere? Are you suggesting that the only way to do this is to use new-object and create a .net regex?
- Perhaps grabbing the script here will help:
http://blog.robbiefoust.com/?p=59
Or read here
http://thepowershellguy.com/blogs/posh/archive/2008/12/30/monday-december-29-2008-5-16-pm-regular-expressions-and-powershell-part-2.aspx
I'm basically doing what you should be doing, and googling ;) - I'm not sure which is more obnoxious... telling me to google or having me read article after article that doesn't apply to what I'm asking for help with. If you don't like my question please don't answer. If I misunderstand the intention of your links please quote the part that shows the answer.I'm not asking for general information about regular expressions or how to form a regex. I'm asking how you can use -replace without a global modifier. Alternatively, I also asked how to get the help page for -replace.... as mentioned I don't even know what to call an operator that uses this -functionname.... I'm having trouble finding any kind of documentation for it.
- Wow - You really get what you pay for in these support forums, don't you?
Hope someone else can help you.
Karl I'm not sure which is more obnoxious... telling me to google or having me read article after article that doesn't apply to what I'm asking for help with. If you don't like my question please don't answer. If I misunderstand the intention of your links please quote the part that shows the answer.
About "-replace":I'm not asking for general information about regular expressions or how to form a regex. I'm asking how you can use -replace without a global modifier. Alternatively, I also asked how to get the help page for -replace.... as mentioned I don't even know what to call an operator that uses this -functionname.... I'm having trouble finding any kind of documentation for it.
PS>get-help about_Comparison_Operators
As Karl mentions, this is free support. We are trying to help. We come across all kinds of people looking for support, and most aren't able to do a simple search to try to look for into on their own. I'll also try to help by just providing links, because I'm very busy and hope that sometimes just providing the right search term might send someone off on the right track (if there were previously completely lost).
So thanks Karl for trying to help anyone who comes here... I really appreciate it.- Marked As Answer byTome Tanasovski Saturday, November 07, 2009 12:11 AM
- I understand the need to post a link to help someone quickly, but why bother if you don't even read the article to see if it applies? That doesn't matter so much as the fact that the google comment came with poor links, and no response to the question about the first links... It's just generally annoying b/c I've spent the time doing the reading, and I'm humble enough to ask for help when I need it only to be greeted with such effrontery.Thank you for your reply... The comparison operator help clearly states that it can only take the two parameters.... so it looks like I won't be able to do what I want to do without creating a .net regex object or using [regex] shortcut.
- Depending on the use-case, you may be able to get away with the following. I realize it may not be as "clean" as you would have previously done in perl, but in a lot of cases, it should be more than sufficient.
> $text="Thanks man, much appreciated"
> $test -replace('m','d')
#results in "Thanks dan, duch appreciated", which isn't what we want
> [regex]::Replace($text, "(?<!m.*)m", "D")
#results in "Thanks Dan, much appreciated", which is what we're looking for.
or, using the example you gave earlier;
> [regex]::Replace("test", "(?<!t.*)t", "k")
kest
Hope that helps,
Dan Holton Use the Regex.Replace(string input, string replacement, int count) overload http://msdn2.microsoft.com/en-us/library/aa332131(VS.71).aspx
PS > $r=[regex]'t'
PS > $r.Replace("test","k",1)
kest
Shay Levy [MVP]
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell ToolbarPS > $r=[regex]'t'
Shay,
PS > $r.Replace("test","k",1)
kest
This is exactly what I wound up doing to get around the problem.
ThanksI understand the need to post a link to help someone quickly, but why bother if you don't even read the article to see if it applies? That doesn't matter so much as the fact that the google comment came with poor links, and no response to the question about the first links... It's just generally annoying b/c I've spent the time doing the reading, and I'm humble enough to ask for help when I need it only to be greeted with such effrontery.
Thank you for your reply... The comparison operator help clearly states that it can only take the two parameters.... so it looks like I won't be able to do what I want to do without creating a .net regex object or using [regex] shortcut.
Tome;
Nowhere in your original question did you mention you did ANY searching except on MSDN. I was trying to help. I am not a regex user, I posted links I thought might provide some help.
Karl- No worries. I appreciate the sentiment. I'm not looking to start a flame war....
- Tome;
Nor am I - I realized later on that the ;) after my mentioning Google might have gone unseen or been misunderstood.
I am glad that Marco was able to help.
Karl

