![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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 |
| | #2 (permalink) | ||||||||||||
| Guest | Re: How to work with regular expressions Jarod wrote:
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 | ||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: How to work with regular expressions "Jarod" <Jarod@xxxxxx> wrote in message news:0FC1C754-9CAE-4F4F-A4F0-F83EE1564E09@xxxxxx
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 | ||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | 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]
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
| ||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | 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
| ||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||
| Guest | Re: How to work with regular expressions > > What if I want to have just those matched numbers?
casting But in .net at least in c# you use (int) not [int].. It would bebetter if they left this like it was ![]() Jedrzej | ||||||||||||||||||||||||
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| regular expressions to replace but keep character? | Nick B | VB Script | 6 | 07-15-2008 03:34 PM |
| YPOPS -- Does it work with regular yahoo mail on Vista? | natifnatal34 | Vista mail | 1 | 07-05-2008 02:29 AM |
| .Net 1.1 moved to Windows Server 2008 Enterprise does not work for regular users, but works for Admins | LVP | Vista security | 4 | 03-24-2008 09:51 AM |
| New lines in regular expressions | Xavier | PowerShell | 7 | 12-25-2007 03:19 PM |
| Regular expressions | Marco Shaw | PowerShell | 1 | 12-19-2006 09:42 AM |