Mokshyam Mokshyam - 1 year ago 223
PowerShell Question

Powershell - Regex - Fixed width file - Escape Characters

While reading a fixed width file like this:

ABC 7112123207/24/16Smith Timpson Head Coach 412-222-0000 00011848660 ELl CAAN HIGH SCHOOL 325 N Peal AVE. Smith Timpson Head Coach COLORADO CITY AZ 86021 01 FALL MALE 07/29/16EQ15031 1977904 BUDDY'S ALL STARS INC. BUDDY ALL STARS N V12V70R16 1.00V12V70R16

I wish to escape the single whitespace, / and the apostrophe.

I tried my regex in Powershell like this:

$Line | Select-String -Pattern "^(.*)[a-zA-Z0-9?\s?]" -AllMatches

I need the match values to read as:


12345607/24/16Joe (this split into three: 123456,07/24/16,Joe)


Head Trainer

Buddy's Box

Answer Source

Match using two or more spaces as a delimiter:

if ($Line -match '(\S.*?)  +(.{6})(.{8})(.*?)  +(.*?)  +(.*?)  +(.*)') {
    $whatsit = $matches[1]
    $index = $matches[2] 
    $date = $matches[3]
    $name1 = $matches[4]
    $name2 = $matches[5]
    $position = $matches[6]
    $place = $matches[7]
} else {
    echo "Bad line $Line"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download