INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Stumped by -Replace with RegEx

Stumped by -Replace with RegEx

(OP)
I have some files with lines of text that will include a date int eh format of Mar 1, 2015. I need to replace any dates with the format of 3/1/2015. I can correctly identify the date with a regular expression, just not sure how I can use a -Replace to change the format. Can anyone assist?

CODE -->

$WordDate = "(?<month>[\dA-Za-z]+) (?<day>\d{1,2}), (?<year>\d{2,4})"
$Line = "4I0139L4               Mar 1, 2015 12:18:58 AM               1 Years" 

RE: Stumped by -Replace with RegEx

(OP)
I am able to replace with the current date, but I need to identify the date that is already in the line that was matched with the regular expression. Any help would be appreciated.

RE: Stumped by -Replace with RegEx

(OP)
Well, I finally got it. There may be a more efficient way to do this and I would be grateful for any suggestions. This does all I needed though. Removed all time strings and replaces dates listed as Month Day, Year as MM/dd/yyyy format then writes the data back to the file.

CODE -->

$FTPContent = (Get-Content C:\Temp\OpsCenter_4I-FTP_23_02_2015_07_00_12_998_AM_12.TSV) 
$WordDate = "(?<month>[\dA-Za-z]+) (?<day>\d{1,2}), (?<year>\d{2,4})"
$TimeRegex = "(1[012]|0?\d):[0-5]?\d(:[0-5]?\d)?\s+[AaPp][Mm]?"
Foreach ($Line in $FTPContent)
{
    $Date = $Line | % {$_ -match $WordDate > $null; $matches[0]}
    $a = Get-Date($Date)
    $a.ToString('MM-dd-yyyy') > $null
    $NewLine = $Line -replace $WordDate, $a
    $NewLine2 = $NewLine -Replace "00:00:00",""
    $NewLine3 = $NewLine2 -Replace $TimeRegex,""
    $NewLine3
    $Report = $Report + $NewLine3 + "`r`n"
}
Set-Content C:\Temp\OpsCenter_4I-FTP_23_02_2015_07_00_12_998_AM_12.TSV $Report 

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: Stumped by -Replace with RegEx

Thanks for sharing this.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close