Powershell Interesting String Operations / Work Around

<########## Work around to remove string with Case In Sensitive Approach ##########>

$full_string = "My name is xyz !!!"
$rm_string = ''
if($full_string -match "XyZ") {
   $index = $full_string.indexof($matches[0])
   if($index -gt 0) {
      $rm_string = $full_string.remove($index)      
   }
}
write-host $rm_string
#Note: I like one liner solution if anybody has

Output:
Case-Sensitive Is Ignored !!!

<######## StartWith Function With Case In Sensitive ########>

$test = "Start word of string"
if($test.StartsWith("stArT","CurrentCultureIgnoreCase") ) {
    Write-Output "Case-Sensitive Is Ignored !!!"
}

Output:
Case-Sensitive Is Ignored !!!

<######## Difference Split String By Word & Character ########>

$test = "Start//word/of/string"

Write-Output "------------------"
Write-Output "Split By Character"
Write-Output "------------------"
$test.split('/') |% {
    Write-Output $_
}

Write-Output "------------------"
Write-Output "Split By Word"
Write-Output "------------------"
$test -split "//" |% {
    Write-Output $_
}

Output:
------------------
Split By Character
------------------
Start

word
of
string
------------------
Split By Word
------------------
Start
word/of/string

<######## Compare Multiple Words In String ########>

$test = ("1 2", "2 3", "1 5", "3 4")
$test |% {
    if($_ -match "3|4") {
        Write-Output $_
    }
}

Output:
2 3
3 4
Advertisements

$matches contains a hashtable (Implicit)

The -match operator matches a pattern and returns a result.  However, along with the result, it also sets the $matches variable which contains the portions of the string that are matched by individual parts of the regular expressions.  $matches contains a hashtable where the keys of the hashtable are indexes that correspond to the parts of the pattern that is matched.  The values are the substrings of the target string that are matched.  Here is an example from the excellent Windows PowerShell in Action by Bruce Payette.

PS C:/> "abcdef" -match "(a) (((b)(c))de)f"
True
PS C:/> $matches
Key        Value
---        -----
5          c
4          b
3          bc
2          bcde
1          a
0          abcdef

You’ll noticed that there is one extra entry in the $matches hashtable beyond the 5 specified patterns.  This is because there is always a default element that represents the entire string that matched.

Since PowerShell is built on .NET regular expressions, you are not limited to index based keys as illustrated above.  This can be a pain when trying figure out which key index matches which matched pattern.  The .NET regular expression implementation allows for named captures by palcing the sequence “?” immediate inside the parenthesis that indicate the matching group.  The above example with named keys would look like this:

PS C:/> "abcdef" -match "(?a) (?((?b)(?c))de)f"
True
PS C:/> $matches
Key        Value
---        -----
o1         c
e3         b
e4         bc
o2         bcde
1          a
0          abcdef

Again Regular Expression !!!!

let’s start with the basic examples

first of all match operator

The -match operator matches a pattern and returns a result so what is -cmatch

-cmatch : Match each character (With case sensitiveness)

-match   : Match each character (Without case sensitiveness)

Go to Console or WindowsPowerShell try out following code

if('Test' -cmatch 'test') { Write-Host "This case insensitiuve"}