Don’t do that #21: Use multiple Replace to select numbers in a string

By | April 21, 2015

Don’t do that: I saw that many times, some people want to extract some numbers in a string and they use the Replace method several times.

or

$log -replace 'backup-full-', '' -replace '-disk-D', ''

It will return ‘22042015’ in this case, but I don’t like this approach because it is “too much” work and in case the prefix/suffix is changed, it does not work.


Do that: I like that one to simply match digits.

[regex]::match($log,'(\d+)').value


previous-buttonNext button blue

One thought on “Don’t do that #21: Use multiple Replace to select numbers in a string

  1. Pingback: Don’t do that #22: Manage a remote server using RDP | Powershell Guru

Leave a Reply

Your email address will not be published. Required fields are marked *