Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

newbie question on regular expressions

$
0
0

I have a file which looks like this:

x: 0
      y:1
z: 4

THe output after parsing should be below. The idea is to split it on colon+space. If the lien begins with white space it is part of the value otherwise it is Id.

ID is x, Value is "0 y:1"

ID is z, Value is "4"

I have the following code for parsing and it doesn't seem to work. I validate the regular expression here and it seems to work.

http://regexpal.com/

My guess is it is syntax. I would appreciate any ideas on this. Thanks,

#$pattern = [regex]"(?m)((\S+)[^:'n]*:(\s+)(.+)'n)((\s+)(.+)'n)*"


#$pattern = [regex]"(?m)((\S+)[^:'n]*:(\s+)(.+)'n)((\s+)(.+)'n)*"
$pattern = [regex]"((\S+)[^:'n]*:(\s+)(.+)'n)((\s+)(.+)'n)*"
$content.split($pattern,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {

$split =  $_.split(":", 2)
    if ($split -eq $null ) {return } 
    if ($split[0] -ne $null)  {$Name=$split[0].Trim()} else {return }
    if ($split[1] -ne $null)  {  $Id = $split[1].Trim() } else {return }
    Write-host "#Name is- $Name #Id is- $Id"
}


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>