0

I am trying to solve this problem using PowerShell and Regex. I have a string variable like the following:

$input = @"
Server Time: 4/13/2016 12:29 PM
Username: RBullwinkle
Lorem ipsum dolor Important Data: rosebud sit amet
"@;

I would like to extract data such that I receive the following result:

$serverTime = "4/13/2016 12:29 PM"
$userName = "RBullwinkle"
$important = "rosebud"

This would be a straightforward regex except for the following complications:

  1. I would like the extraction to be robust to any changes in the order of the data.
  2. I would like to process the input string in a single pass.
1
  • 1
    For data that is structured nicely like this use ConvertFrom-StringData. You can do that with very little regex. Just need to change the : to = and it should work. I have a few posts about this approach. Commented Apr 13, 2016 at 22:47

2 Answers 2

1

credit goes to @sweaver2112 for the regex work, but i thought i'd add this to show one way to convert this information into a powershell object

$input1 = @'
Server Time: 4/13/2016 12:29 PM
Username: RBullwinkle
Lorem ipsum dolor Important Data: rosebud sit amet
'@

$input2 = @'
Lorem ipsum dolor Important Data: daffodils are lovely
Server Time: 4/13/2016 12:29 PM
Username: RBullwinkle
'@

$regex = 'Server\sTime:\s?(?<servertime>.*)|Username:\s?(?<username>.*)|Important\sData:\s?(?<importantdata>\w+)'
$matches1 = [regex]::matches($input1, $regex)
$matches2 = [regex]::matches($input2, $regex)

$hash1 = @{}
$matches1 | % {
    if ($_.Success) {
        $value = $_.value -split ': '
            $hash1.Add($value[0].trim(), $value[1].trim())
    }
}

$hash2 = @{}
$matches2 | % {
    if ($_.Success) {
        $value = $_.value -split ': '
            $hash2.Add($value[0].trim(), $value[1].trim())
    }
}

[pscustomobject]$hash1, [pscustomobject]$hash2
Sign up to request clarification or add additional context in comments.

Comments

1

you could use .NET framework regular expressions that will let you do a global (do not return on first match) match, combined with alternation to match what you want. Regex demo is easily shown (check your match info), the powershell commands might be something like this (admittedly, I'm not a powershell guy):

$myinput = @"
Server Time: 4/13/2016 12:29 PM
Username: RBullwinkle
Lorem ipsum dolor Important Data: rosebud sit amet
"@
$myotherinput = @"
Lorem ipsum dolor Important Data: daffodils are lovely
Server Time: 4/13/2016 12:29 PM
Username: RBullwinkle
"@

$pattern = "Server\sTime:\s?(?<servertime>.*)|Username:\s?(?<username>.*)|Important\sData:\s?(?<importantdata>\w+)"
$mymatches = [regex]::matches($myinput, $pattern)
$myothermatches = [regex]::matches($myotherinput, $pattern)

$mymatches | ForEach-Object { if ( $_.Success) { echo $_.value}}

#Server Time: 4/13/2016 12:29 PM
#Username: RBullwinkle
#Important Data: rosebud

$myothermatches | ForEach-Object { if ( $_.Success) { echo $_.value}}

#Important Data: daffodils
#Server Time: 4/13/2016 12:29 PM
#Username: RBullwinkle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.