17

I am having some issues with regular expression mainly because I think the information I can find is not specifically for powershell and all the samples I have tried either error or don't work as intended. I am trying to replace the first occurrence of a word in a string with another word but not replace any other occurrences of that word. for an example take the string:

My name is Bob, her name is Sara.

I would like to replace the first occurrence of name with baby so the resulting string would be

My baby is Bob, her name is Sara.

I have been working in https://regex101.com/ to try to build and see what is selected as I go but as I said none of these have a powershell flavor of regex. In that I can just turn off the global flag and it seems to select the first occurrence but not in powershell. So I am really at a loss of where to begin all really have at this point is selecting all occurrences of the word namewith:

$test = "My name is Bob, her name is Sara."
$test -replace 'name', 'baby'
themackyo
  • 363
  • 1
  • 4
  • 13

2 Answers2

42

One way to replace n times:

$test = "My name is Bob, her name is Sara."
[regex]$pattern = "name"
$pattern.replace($test, "baby", 1) 

> My baby is Bob, her name is Sara
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • So the `1` Just says replace the first one you find ? A `2` would replace both then ? If you said `3` and only have 2 words of `name` would it error or only replace the first 2 ? – themackyo Oct 17 '16 at 15:56
  • Yes, its the number of replacements to make. If you pass a number greater then the actual frequency of the word it will just replace them all. – Alex K. Oct 17 '16 at 16:05
  • This is a much more intuitive way to do this, and works with variables much better. – J E Carter II Nov 06 '19 at 16:32
  • The syntax with the square brackets is not intuitive for me. Could someone point me to the documentation. – MauiMuc Jun 25 '22 at 10:20
  • @MauiMuc `[Type]` in front of a variable definition applies a _type constraint_. It restricts what kind of data can be assigned to the variable. If the value on the right-hand-side can be converted to the type, then the conversion is applied, in this case `String` to `Regex`. See [this answer](https://stackoverflow.com/a/66996505/7571258) for details. – zett42 Jul 24 '22 at 18:59
6

You could capture everything before and behind and replace it:

'My name is Bob, her name is Sara.' -replace '(.*?)name(.*)', '$1baby$2'
Tony
  • 2,658
  • 2
  • 31
  • 46
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Wow that worked great. I think I was close to that I tried some examples like that with `$1` and `$2` but couldn't get it to work properly. Thanks for the help. I didn't fully understand how the examples work but this makes a lot of sense now. – themackyo Oct 17 '16 at 15:07
  • 1
    Nice! Using the anchor `^` for the start of the string, the 2nd capture group could be omitted: `'My name is Bob, her name is Sara.' -replace '^(.*?)name', '$1baby'`. See [regex101 demo](https://regex101.com/r/ojgKte/1) – zett42 Jul 24 '22 at 18:49