I have a CSV StackOverflow-style coded URLs, and need to convert them to HTML with Powershell and save them back in the third column of the CSV.
CSV Sample:
ID,Comment,CommentConverted
1,Check [this out](https://stackoverflow.com),
2,To buy coffee [click here](https://google.com) or [here](https://bing.com),
What I need
cID,Comment,CommentConverted
1,Check [this out](https://stackoverflow.com),Check <a href="https://stackoverflow.com">this out</a>
2,To buy coffee [click here](https://google.com) or [here](https://bing.com),To buy coffee <a href="https://google.com">Click Here</a> or <a href="https://bing.com">here</a>
I'm having problems parsing this where there are multiple URLs in the Comment
field.
My (ugly, highly-verbose) Powershell code:
$comment_list = Import-Csv "c:\temp\Comments.csv"
$comment_list.foreach(
{
$x = $_.Comment
$linktextOUT = $x.Substring($x.IndexOf('[')+1,$x.IndexOf(']')-$x.IndexOf('[')-1)
$linktextREPLACE = "[" + $linktextOUT + "]"
$URLOUT = $x.Substring($x.IndexOf('(')+1,$x.IndexOf(')')-$x.IndexOf('(')-1)
$URLREPLACE = "(" + $URLOUT + ")"
$output = $x.Replace($URLREPLACE, "")
$output = $output.Replace($linktextREPLACE, "")
$output = $output + "<a href=""" + $URL + """>" + $linktext + "</a>"
Write-Host $_.cID","$_.Comment","$output
})
What it outputs:
1 , Check [this out](https://stackoverflow.com) , Check <a href="https://stackoverflow.com">this out</a>
2 , To buy coffee [click here](https://google.com) or [here](https://bing.com) , To buy coffee or [here](https://bing.com)<a href="https://google.com">click here</a>
You can see that the first line is just fine - the Powershell outputs the correct third column. However, it can't see the second URL in line 2, so it completely skips the "bing.com" URL. Note that some lines may have up to 5 URLs in the comments.
Any help is appreciated in advance.