Is there case-insensitive option for "-replace" and "[contains]" ?

rated by 0 users
Not Answered This post has 0 verified answers | 3 Replies | 2 Followers

Not Ranked
2 Posts
nickolasha posted on 04-25-2012 8:43 AM


I need to know is there case-insensitive option for this:

$files=Get-ChildItem "D:\temp\"* -include *.resx -recurse 
          $xml=[xml](Get-Content $_)
               ForEach-Object{$_.'#text' = ($_.'#text' -replace 'text','foo1') }               

I want to do replace in xml resx files with Powershell. This script work only like case-sensitive("text" must match exactly).

Can I modify this script to make case-insensitive replacement?

Thank you in forward.

All Replies

Top 10 Contributor
640 Posts

Use the Replace() method of a string object


It's case-sensitive already. 

You aren't using the -contains operator; I'm not familiar with the [contains] syntax you're using. That looks to be part of XPath, and so it would already be case-sensitive. But it isn't the PowerShell -contains operator, if that's what you're thinking. PowerShell isn't processing that; XML is.

Not Ranked
2 Posts

Thank you for reply.

Now I need to count entries which matched by pattern in resx xml files. In output I need integer value of matched entries by pattern inside //data/value xpath (and maybe also //data/comment for include also comments).

Maybe I can do something like that?

var counter = 0
get-childitem "D:\temp\"* -include *.resx -recurse -force 
| foreach-object { $xml=[xml](get-content $_.FullName) 
| foreach-object{$_.'#text' -match  'text' 
if($matches.count != 0) { counter += $matches.count } } 
| select counter }

or something like that:

var counter = 0
get-childitem "D:\temp\"* -include *.resx -recurse -force
 | select-xml -xpath "//data/value" 
 | foreach-object { $_.node.InnerXML -match '[\s\S]*?text[\s\S]*?' 
   if($matches.count != 0) { counter += $matches.count } 
 | select counter
Top 10 Contributor
640 Posts

$matches.count will include a count of all matches made from the last -match operation, yes. 

Page 1 of 1 (4 items) | RSS
Copyright 2012 All rights reserved.