Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to work with regular expressions

Closed Thread
 
Thread Tools Display Modes
Old 12-17-2007   #1 (permalink)
Jarod
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

Old 12-17-2007   #2 (permalink)
Marco Shaw [MVP]
Guest


 

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
Old 12-17-2007   #3 (permalink)
Keith Hill [MVP]
Guest


 

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



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

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


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
>

Old 12-18-2007   #6 (permalink)
Jarod
Guest


 

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

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








Vistax64.com 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 2005-2008

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 47 48 49 50