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

RB

Vista - Changing guids to uppercase

Reply
 
03-20-2007   #1
p2


 
 

Changing guids to uppercase

I have a file (XML file) that has GUIDs sprinkled througout it. I would like
to convert all the GUIDs to upper-case. I would have thought that this would
work but I can't quite figure it out.

(get-content filename) -replace "[0-9a-fA-F-]{36}", ([string]'$0').toupper()

The regex expression is matching correctly but the $0.toupper() simply isn't
working. What am I missing?

Any hints appreciated...

My System SpecsSystem Spec
03-21-2007   #2
Keith Hill


 
 

Re: Changing guids to uppercase

"p2" <p2@discussions.microsoft.com> wrote in message
newsF01B5A9-9EAD-44A4-A481-8A07E725F3E6@microsoft.com...
>I have a file (XML file) that has GUIDs sprinkled througout it. I would
>like
> to convert all the GUIDs to upper-case. I would have thought that this
> would
> work but I can't quite figure it out.
>
> (get-content filename) -replace "[0-9a-fA-F-]{36}",
> ([string]'$0').toupper()
>
> The regex expression is matching correctly but the $0.toupper() simply
> isn't
> working. What am I missing?
>
> Any hints appreciated...


Well I can tell you why it is not working but I haven't come up with an easy
solution to the problem. The reason it doesn't work is that the $1 (should
be 1 instead of 0) is only evaluated by the regex engine. So $1 isn't
meaningful to PowerShell (most like is null) so calling ToUpper only results
in an empty replacement string. Now if -replace supported a scriptblock
that would be called for match evaluation (which is supported by .NET) then
something like this would be easy:

# !!!!! THIS DOES NOT WORK IN POWERSHELL 1.0
$hexCh = "[a-fA-F0-9]"
$guidPattern =
[regex]"(?<guid>$hexCh{8}-$hexCh{4}-$hexCh{4}-$hexCh{4}-$hexCh{12})"
(get-content filename) -replace "$guidPattern", {$match.guid.ToUpper()}

Now that would be wicked cool if this support were added. I think I will
file this as an enhancement request on the connect site alongside my request
for -matchall.

Maybe somebody who is a bit more regex savy can come up with a regex way to
capitalize with the .NET regex engine but since the examples for the
MatchEvaluator delegates were demonstrating capitalization, I kind of doubt
it. If you knew there was ever only one GUID per line then it would
probably be easy to use -match to grab the GUID via the $matches variable
and then capitalize that guid. Then turn around and use replace on the same
line to replace the guid with the capitalized version.

--
Keith

My System SpecsSystem Spec
03-21-2007   #3
Keith Hill


 
 

Re: Changing guids to uppercase

"Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
news:O5bG6u3aHHA.348@TK2MSFTNGP02.phx.gbl...
> "p2" <p2@discussions.microsoft.com> wrote in message
> newsF01B5A9-9EAD-44A4-A481-8A07E725F3E6@microsoft.com...
> in an empty replacement string. Now if -replace supported a scriptblock
> that would be called for match evaluation (which is supported by .NET)
> then something like this would be easy:
>
> # !!!!! THIS DOES NOT WORK IN POWERSHELL 1.0
> $hexCh = "[a-fA-F0-9]"
> $guidPattern =
> [regex]"(?<guid>$hexCh{8}-$hexCh{4}-$hexCh{4}-$hexCh{4}-$hexCh{12})"
> (get-content filename) -replace "$guidPattern", {$match.guid.ToUpper()}
>
> Now that would be wicked cool if this support were added.


If anyone else likes this idea you can vote on it here:

https://connect.microsoft.com/feedba...4447&SiteID=99

--
Keith

My System SpecsSystem Spec
03-21-2007   #4
Andrew Savinykh


 
 

Re: Changing guids to uppercase

There is a blog entry from Bruce Payette:
http://blogs.msdn.com/powershell/arc...25/678259.aspx

I think once powershell supports an easier way to work with delegates,
the standard MatchEvaluator solution will do. The advantage is that it
leverages existing .net paradigm so it is well known. Adding an ability
to specify MatchEvaluator in the -replace operator may be nice too.

//Andrew

Keith Hill wrote:
> "p2" <p2@discussions.microsoft.com> wrote in message
> newsF01B5A9-9EAD-44A4-A481-8A07E725F3E6@microsoft.com...
>> I have a file (XML file) that has GUIDs sprinkled througout it. I
>> would like
>> to convert all the GUIDs to upper-case. I would have thought that this
>> would
>> work but I can't quite figure it out.
>>
>> (get-content filename) -replace "[0-9a-fA-F-]{36}",
>> ([string]'$0').toupper()
>>
>> The regex expression is matching correctly but the $0.toupper() simply
>> isn't
>> working. What am I missing?
>>
>> Any hints appreciated...

>
> Well I can tell you why it is not working but I haven't come up with an
> easy solution to the problem. The reason it doesn't work is that the $1
> (should be 1 instead of 0) is only evaluated by the regex engine. So $1
> isn't meaningful to PowerShell (most like is null) so calling ToUpper
> only results in an empty replacement string. Now if -replace supported
> a scriptblock that would be called for match evaluation (which is
> supported by .NET) then something like this would be easy:
>
> # !!!!! THIS DOES NOT WORK IN POWERSHELL 1.0
> $hexCh = "[a-fA-F0-9]"
> $guidPattern =
> [regex]"(?<guid>$hexCh{8}-$hexCh{4}-$hexCh{4}-$hexCh{4}-$hexCh{12})"
> (get-content filename) -replace "$guidPattern", {$match.guid.ToUpper()}
>
> Now that would be wicked cool if this support were added. I think I
> will file this as an enhancement request on the connect site alongside
> my request for -matchall.
>
> Maybe somebody who is a bit more regex savy can come up with a regex way
> to capitalize with the .NET regex engine but since the examples for the
> MatchEvaluator delegates were demonstrating capitalization, I kind of
> doubt it. If you knew there was ever only one GUID per line then it
> would probably be easy to use -match to grab the GUID via the $matches
> variable and then capitalize that guid. Then turn around and use
> replace on the same line to replace the guid with the capitalized version.
>
> --
> Keith

My System SpecsSystem Spec
03-21-2007   #5
/\/\o\/\/ [MVP]


 
 

Re: Changing guids to uppercase

did go the same way (matchevaluator and post by Bruce, as you can use the
delegate directly.

it will work now, as you do not need the match (ok not so usefull but as
example)
but to catch that you need to change the delegate as show in the post by
Bruce.

PoSH> [regex]::Replace("foo",".*",{get-date}.invoke())
03/21/2007 11:29:1703/21/2007 11:29:17

Greetings /\/\o\/\/

"Andrew Savinykh" wrote:

> There is a blog entry from Bruce Payette:
> http://blogs.msdn.com/powershell/arc...25/678259.aspx
>
> I think once powershell supports an easier way to work with delegates,
> the standard MatchEvaluator solution will do. The advantage is that it
> leverages existing .net paradigm so it is well known. Adding an ability
> to specify MatchEvaluator in the -replace operator may be nice too.
>
> //Andrew
>
> Keith Hill wrote:
> > "p2" <p2@discussions.microsoft.com> wrote in message
> > newsF01B5A9-9EAD-44A4-A481-8A07E725F3E6@microsoft.com...
> >> I have a file (XML file) that has GUIDs sprinkled througout it. I
> >> would like
> >> to convert all the GUIDs to upper-case. I would have thought that this
> >> would
> >> work but I can't quite figure it out.
> >>
> >> (get-content filename) -replace "[0-9a-fA-F-]{36}",
> >> ([string]'$0').toupper()
> >>
> >> The regex expression is matching correctly but the $0.toupper() simply
> >> isn't
> >> working. What am I missing?
> >>
> >> Any hints appreciated...

> >
> > Well I can tell you why it is not working but I haven't come up with an
> > easy solution to the problem. The reason it doesn't work is that the $1
> > (should be 1 instead of 0) is only evaluated by the regex engine. So $1
> > isn't meaningful to PowerShell (most like is null) so calling ToUpper
> > only results in an empty replacement string. Now if -replace supported
> > a scriptblock that would be called for match evaluation (which is
> > supported by .NET) then something like this would be easy:
> >
> > # !!!!! THIS DOES NOT WORK IN POWERSHELL 1.0
> > $hexCh = "[a-fA-F0-9]"
> > $guidPattern =
> > [regex]"(?<guid>$hexCh{8}-$hexCh{4}-$hexCh{4}-$hexCh{4}-$hexCh{12})"
> > (get-content filename) -replace "$guidPattern", {$match.guid.ToUpper()}
> >
> > Now that would be wicked cool if this support were added. I think I
> > will file this as an enhancement request on the connect site alongside
> > my request for -matchall.
> >
> > Maybe somebody who is a bit more regex savy can come up with a regex way
> > to capitalize with the .NET regex engine but since the examples for the
> > MatchEvaluator delegates were demonstrating capitalization, I kind of
> > doubt it. If you knew there was ever only one GUID per line then it
> > would probably be easy to use -match to grab the GUID via the $matches
> > variable and then capitalize that guid. Then turn around and use
> > replace on the same line to replace the guid with the capitalized version.
> >
> > --
> > Keith

>

My System SpecsSystem Spec
03-21-2007   #6
/\/\o\/\/ [MVP]


 
 

Re: Changing guids to uppercase

forget example plz ;-) as trhis is same as (get-date)

"/\/\o\/\/ [MVP]" wrote:

> did go the same way (matchevaluator and post by Bruce, as you can use the
> delegate directly.
>
> it will work now, as you do not need the match (ok not so usefull but as
> example)
> but to catch that you need to change the delegate as show in the post by
> Bruce.
>
> PoSH> [regex]::Replace("foo",".*",{get-date}.invoke())
> 03/21/2007 11:29:1703/21/2007 11:29:17
>
> Greetings /\/\o\/\/
>
> "Andrew Savinykh" wrote:
>
> > There is a blog entry from Bruce Payette:
> > http://blogs.msdn.com/powershell/arc...25/678259.aspx
> >
> > I think once powershell supports an easier way to work with delegates,
> > the standard MatchEvaluator solution will do. The advantage is that it
> > leverages existing .net paradigm so it is well known. Adding an ability
> > to specify MatchEvaluator in the -replace operator may be nice too.
> >
> > //Andrew
> >
> > Keith Hill wrote:
> > > "p2" <p2@discussions.microsoft.com> wrote in message
> > > newsF01B5A9-9EAD-44A4-A481-8A07E725F3E6@microsoft.com...
> > >> I have a file (XML file) that has GUIDs sprinkled througout it. I
> > >> would like
> > >> to convert all the GUIDs to upper-case. I would have thought that this
> > >> would
> > >> work but I can't quite figure it out.
> > >>
> > >> (get-content filename) -replace "[0-9a-fA-F-]{36}",
> > >> ([string]'$0').toupper()
> > >>
> > >> The regex expression is matching correctly but the $0.toupper() simply
> > >> isn't
> > >> working. What am I missing?
> > >>
> > >> Any hints appreciated...
> > >
> > > Well I can tell you why it is not working but I haven't come up with an
> > > easy solution to the problem. The reason it doesn't work is that the $1
> > > (should be 1 instead of 0) is only evaluated by the regex engine. So $1
> > > isn't meaningful to PowerShell (most like is null) so calling ToUpper
> > > only results in an empty replacement string. Now if -replace supported
> > > a scriptblock that would be called for match evaluation (which is
> > > supported by .NET) then something like this would be easy:
> > >
> > > # !!!!! THIS DOES NOT WORK IN POWERSHELL 1.0
> > > $hexCh = "[a-fA-F0-9]"
> > > $guidPattern =
> > > [regex]"(?<guid>$hexCh{8}-$hexCh{4}-$hexCh{4}-$hexCh{4}-$hexCh{12})"
> > > (get-content filename) -replace "$guidPattern", {$match.guid.ToUpper()}
> > >
> > > Now that would be wicked cool if this support were added. I think I
> > > will file this as an enhancement request on the connect site alongside
> > > my request for -matchall.
> > >
> > > Maybe somebody who is a bit more regex savy can come up with a regex way
> > > to capitalize with the .NET regex engine but since the examples for the
> > > MatchEvaluator delegates were demonstrating capitalization, I kind of
> > > doubt it. If you knew there was ever only one GUID per line then it
> > > would probably be easy to use -match to grab the GUID via the $matches
> > > variable and then capitalize that guid. Then turn around and use
> > > replace on the same line to replace the guid with the capitalized version.
> > >
> > > --
> > > Keith

> >

My System SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: Changing guids to uppercase
Thread Forum
managing files using volume GUIDs PowerShell
Typewriting lowercase after uppercase letters quickly Vista performance & maintenance
media changing delays when changing tracks Vista music pictures video
Duplicate GUIDS... Vista installation & setup
Duplicate GUIDs... Vista installation & setup


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