1

So I have this PowerShell script that will replace a Youtube URL by replacing the "https://www.youtube.com/watch?v=" to "id=" without removing the id of the URL.

example:
copyboard variable before: https://www.youtube.com/watch?v=FigUL6NWtqw
copyboard variable then: id=FigUL6NWtqw

Here is what I already tried:

powershell

$Copyboard = Get-Clipboard | Out-String
$Copyboard = $Copyboard -replace "https://www.youtube.com/watch?v=", "id="
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
BOOM
  • 13
  • 4
  • 2
    1st - DO NOT use `Out-String` since it sometimes adds things like newlines to the source. plus, your clipboard input is likely already a string. ///// 2nd - your URL has regex special characters in it [the period, for instance] that need to be escaped before you can use `-replace` on it. that operator uses regex. you can use `[regex]::Escape()` on the URL ... or you can use the `.Replace()` string method instead - it does NOT use regex. plus, there are other ways ... splitting the string, for instance. [*grin*] – Lee_Dailey Jun 16 '19 at 11:56

3 Answers3

2

The Regex -Replace operator can be used to achieve the results. This replaces everything up until the first = character with id.

$Copyboard = $Copyboard -Replace "^[^=]+","id"

Using the -Split operator, you can prepend id= to the query value. I am splitting with the = character as the delimiter and returning all data after the = with the [1] index. Here I used the format operator (-f), but you can use any concatenation technique you choose.

$Copyboard = "{0}{1}" -f 'id=',($Copyboard -Split "=")[1]

An alternative approach is to first cast $Copyboard as a [uri], which opens up other options for your code later only if you don't reassign $Copyboard with the new value. Then access the property (query in this case) you want to change and only output that changed property. With the uri object, the different parts of the copied uri are broken down into subparts and the resulting subparts are stored as properties of the object.

Using the regex -Replace operator with the uri object:

([uri]$copyboard).Query -Replace "^[^=]+","id"
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
1

There's some regex characters in the source string, '.' and '?'. Mostly the question mark messes you up. (without "| out-string")

$a = 'https://www.youtube.com/watch?v=FigUL6NWtqw'     
[regex]::escape('https://www.youtube.com/watch?v=')                   

https://www\.youtube\.com/watch\?v=

You can use the escaped version:

$a -replace [regex]::escape('https://www.youtube.com/watch?v='), 'id='

Or backslash the ?:

$a -replace 'https://www.youtube.com/watch\?v=', 'id='

Or the .net .replace() that doesn't use regex:

$a.replace('https://www.youtube.com/watch?v=', 'id=')
js2010
  • 23,033
  • 6
  • 64
  • 66
0

It sounds like you simply need to extract the last =-based token from your input string, which is most easily (though not most quickly) done with the -split operator:

$Copyboard = 'id=' + ((Get-Clipboard -Format Text) -split '=')[-1]

Note the use of -Format Text to ensure that text is retrieved from the clipboard.

Index [-1] retrieves the last element from the array of tokens that results from splitting the string by = with -split '=', which is FigUL6NWtqw with your sample input.

As an aside: it makes no difference here, but note that PowerShell's -split operator, unlike the .NET .Split() method, splits by regular expressions, and generally is more powerful than .Split() - see this answer for more.

mklement0
  • 382,024
  • 64
  • 607
  • 775