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

rated by 0 users
This post has 3 Replies | 2 Followers

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

Hello.

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

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

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.
Top 10 Contributor
Posts 640

Use the Replace() method of a string object

$_.'#text'.Replace('text','fool')

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

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) 
$xml.selectNodes('//data/value[text()]') 
| 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
Posts 640

$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 PowerShell.com. All rights reserved.