![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | How to work with regular expressions Hi! When I do: dir | where { $_.Name -match "\d+" } It displays names containing numbers. What if I want to have just those matched numbers? So if the file name is : powerShell_01.txt I want to have this 01 how to get this? Later on I'd like to convert it to a number so if it's 0001 or 001 or 000001 it's just 1. and rename files ![]() Thx for help with this. Jedrzej |
My System Specs![]() |
| | #2 (permalink) |
| | Re: How to work with regular expressions Jarod wrote: Quote: > Hi! > > When I do: > > > dir | where { $_.Name -match "\d+" } > > It displays names containing numbers. > What if I want to have just those matched numbers? > > So if the file name is : powerShell_01.txt > I want to have this 01 how to get this? > > Later on I'd like to convert it to a number so if it's > 0001 or 001 or 000001 it's just 1. > > and rename files ![]() > > Thx for help with this. > > Jedrzej > http://safari.oreilly.com/9780596528...sion_reference Is definitely a good reference! -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: How to work with regular expressions "Jarod" <Jarod@xxxxxx> wrote in message news:0FC1C754-9CAE-4F4F-A4F0-F83EE1564E09@xxxxxx Quote: > Hi! > > When I do: > > > dir | where { $_.Name -match "\d+" } Any match with the operator -match updates a global called $matches. Any capture groups (named or unnamed) are created as properties on this object. $matches[1] would be the first unnamed capture group. -- Keith |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How to work with regular expressions > What if I want to have just those matched numbers? You can access what's been matched by your entire pattern with $matches[0] Quote: > Later on I'd like to convert it to a number so if it's you can conver a number to a string using .tostring but can't convert a string to a number using .tonumber (no such thing). Instead, do this: $i = "00001" $numeric_i = [int] $i Mike "Jarod" <Jarod@xxxxxx> wrote in message news:0FC1C754-9CAE-4F4F-A4F0-F83EE1564E09@xxxxxx Quote: > Hi! > > When I do: > > > dir | where { $_.Name -match "\d+" } > > It displays names containing numbers. > What if I want to have just those matched numbers? > > So if the file name is : powerShell_01.txt > I want to have this 01 how to get this? > > Later on I'd like to convert it to a number so if it's > 0001 or 001 or 000001 it's just 1. > > and rename files ![]() > > Thx for help with this. > > Jedrzej > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How to work with regular expressions ## create test files 1..9 | foreach {new-item -path ".\powershell_0$_.txt" -itemtype file -force} Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 17/12/2007 21:22:03 0.0 B powershell_01.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_02.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_03.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_04.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_05.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_06.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_07.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_08.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_09.txt ## get all files and rename dir powershell_*.* | foreach { [int]$num = ([regex]'\d+').match($_.name).value rename-item $_.name "powershell_$num.txt" -force } dir powershell_*.* Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 17/12/2007 21:22:03 0.0 B powershell_1.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_2.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_3.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_4.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_5.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_6.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_7.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_8.txt -a--- 17/12/2007 21:22:03 0.0 B powershell_9.txt ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > Hi! > > When I do: > > dir | where { $_.Name -match "\d+" } > > It displays names containing numbers. What if I want to have just > those matched numbers? > > So if the file name is : powerShell_01.txt > I want to have this 01 how to get this? > Later on I'd like to convert it to a number so if it's 0001 or 001 or > 000001 it's just 1. > > and rename files ![]() > > Thx for help with this. > > Jedrzej > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: How to work with regular expressions > > What if I want to have just those matched numbers? Quote: > > You can access what's been matched by your entire pattern with $matches[0] > Quote: > > Later on I'd like to convert it to a number so if it's > I spent ages trying to figure out how to do this. It seemed odd to me that > you can conver a number to a string using .tostring but can't convert a > string to a number using .tonumber (no such thing). Instead, do this: > > $i = "00001" > $numeric_i = [int] $i > casting But in .net at least in c# you use (int) not [int].. It would bebetter if they left this like it was ![]() Jedrzej |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| regular expressions - why are they so hard to compose and decipher? | PowerShell | |||
| $Variables into Regular expressions ?! help | PowerShell | |||
| regular expressions to replace but keep character? | VB Script | |||
| New lines in regular expressions | PowerShell | |||
| Regular expressions | PowerShell | |||