Skip to content

Instantly share code, notes, and snippets.

@jpogran
Created April 27, 2011 12:46
Show Gist options
  • Save jpogran/944187 to your computer and use it in GitHub Desktop.
Save jpogran/944187 to your computer and use it in GitHub Desktop.
parse arbitrary text
#parse a newline deliminated document that has an uppercase
#primary 'header' and several lines of secondary 'items'
Function Parse-Document{
param([string]$path)
$text = gc $path
foreach($line in $text){
if(IsUpperCase $line){
#"Primary: $line";
$spec = new-object PSObject -property @{ Primary = $line; Secondary = @() }
[void]$foreach.MoveNext();
do{
[string]$current = $foreach.Current
#"Secondary: $current";
$spec.Secondary+= $current;
[void]$foreach.MoveNext();
}while( [string]::IsNullOrEmpty($foreach.current) -eq $false )
$spec
}
}
}
Function IsUppercase{
param([string]$str)
#[bool]$upper; #to hold return value
[string]$pattern = "[a-z]"; #variable to hold the search pattern
[Regex]$AllCaps = new-object Regex($pattern);
if($AllCaps.IsMatch($str))
{
$upper = $false;
}else{
$upper = $true;
}
$upper;
}
$info = Parse-Document .\phy_specialties.txt
foreach($i in $info){
"primary = new Primary(){ Name = `"$($i.Primary)`" };"
"secondaries = new List<Secondary>();"
foreach($sec in $i.Secondary){
@"
secondaries.Add(new Secondary()
{
Name = `"$($sec)`"
});
"@
}
"context.primaries.Add(physicianSpecialty);"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment