Question

Tom1Jerry02 --> 02,
abcd0asdf001 --> 001,
qwerty1 --> 1

I am new to Powershell and wanted to know: how I can extract the integers at the end of the string from the above sample using power shell?

Edit:

These are 3 separate strings Tom1Jerry02, abcd0asdf001 and qwerty1. I need a solution suitable for those kind of strings

Était-ce utile?

La solution

With PowerShell you're likely to find many different ways to do the same thing. Here's a way to do this as one-liner:

PS> 'Tom1Jerry02','abcd0asdf001','qwerty1' | Foreach {if ($_ -match '(\d+)$') {$matches[1]}}
02
001
1

Autres conseils

PS> $a =  "Tom1Jerry02 --> 02", "abcd0asdf001 --> 001", "qwerty1 --> 1"
PS> foreach ($s in $a) {
>> $b = $s -Match "(\d+)$"
>> write-output $matches[1]
>> }
>>

Output:

02
001
1
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top