Reputation: 71
In Powershell, I have been working with regex and trying to trim strings based on where the time is in it. Currently, a person inputs the string manually so it can come in multiple ways. The format of the data is this
$appointment = "Certified Substitute Orientation (Session 1) - 9:00 AM - 11:00 AM"
$appointment = "TBI 101 1:00 pm - 3:00 pm"
The titles change and the times could be resprented differently every time. It could be: 9am, 9 am, 9:00, etc. and I want to try and find a way to key off of the time, because it won't always be in the same place.
I am a novice at this stuff. Any help would be greatly appreciated!
Thank you.
I know this is a lot to ask, but I have tried messing with code that looks like this and can't seem to get it to work:
If ($appointment -like "*Session 1*") {
$appointment1 = $appointment
$appointment = $appointment -creplace '[^1-9]*'
$appointment=$appointment.substring(0,$appointment.subject.length-1)
$appointment1 = $appointment1 -replace '-' -replace "[(]","" -replace "[)]","" -replace "event","" -replace "'" -replace '[,]',' ' -replace " "," " -replace " "," " -replace '[\d+]:.*'
}
My goal is to split the string right before the time so that I have:
$appointment1 = "Certified Substitute Orientation (Session 1)"
$appointment2 = "9:00 AM - 11:00 AM"
Upvotes: 0
Views: 83
Reputation:
With a more complex RegEx you can even separate the session:
## Q:\Test\2019\08\22\SO_57614115.ps1
$appointments = @"
Certified Substitute Orientation (Session 1) - 9:00 AM - 11:00 AM
TBI 101 1:00 pm - 3:00 pm
"@ -split '\r?\n'
$RE = '^(?<title>.*?)[-_ ]*(?<session>\([^\)]*\))?[-_ ]*(?<from>1?\d:[0-5]\d\s*[ap]m)[-_ ]+(?<to>1?\d:[0-5]\d\s*[ap]m)$'
Foreach($appointment in $appointments){
if ($appointment -match $RE){
[PSCustomObject]@{
Title = $Matches.title
Session = $Matches.session
From = $Matches.from.ToUpper()
To = $Matches.to.ToUpper()
}
} else {
"doesn't match RE: $appointment"
}
}
the script yields:
> Q:\Test\2019\08\22\SO_57614115.ps1
Title Session From To
----- ------- ---- --
Certified Substitute Orientation (Session 1) 9:00 AM 11:00 AM
TBI 101 1:00 PM 3:00 PM
Upvotes: 1