Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to work with regular expressions

Reply
 
Old 12-17-2007   #1 (permalink)
Jarod


 
 

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 SpecsSystem Spec
Old 12-17-2007   #2 (permalink)
Marco Shaw [MVP]


 
 

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
>
As Shay posted in an earlier thread:
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 SpecsSystem Spec
Old 12-17-2007   #3 (permalink)
Keith Hill [MVP]


 
 

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+" }
dir | %{if ($_.name -match '(\d+)') { $matches[1] }}

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 SpecsSystem Spec
Old 12-17-2007   #4 (permalink)
Mike Miller


 
 

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
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

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 SpecsSystem Spec
Old 12-17-2007   #5 (permalink)
Shay Levi


 
 

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 SpecsSystem Spec
Old 12-18-2007   #6 (permalink)
Jarod


 
 

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
>
How can i find such things in manual ? It looks like old good .net type
casting But in .net at least in c# you use (int) not [int].. It would be
better if they left this like it was

Jedrzej
My System SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46