1

I have a self generated HTML file (in a local directory) with all the body on one line:

<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>server - path</title></head><body><H1>server - path</H1><hr>
<pre><A HREF="/logs/folder/">[To Parent Directory]</A><br><br> jeudi 5 janvier 2017 19:38 116483 <A HREF="/folder/file1.csv">file1.csv</A><br> jeudi 5 janvier 2017 19:39 138397 <A HREF="/folder/file2.csv">file2.csv</A></A><br></pre><hr></body></html>

And I need to extract the name of the file and date. I succeed to read the right line. But I'm blocked to split the line on <br>.

I try something like this:

$string = "first line<br>second line <br> third line<br> end<br>"
write-host $string
$separator = "<br>"
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$string.Split($separator, $option)

But I have that for result :

first line<br>second line <br> third line<br> end<br>
fi
st line
second line
thi
d line
end

I see the HTML Agility Pack, but in my case, I don't have any tag in my page.

Do you have any advice? Thanks!

Bernard Vander Beken
5,1365 gold badges56 silver badges80 bronze badges
asked Jan 20, 2017 at 9:10
1

1 Answer 1

3

The String.Split() method takes your string <br> and treats it as a [char] array, splitting on every single occurrence of either <, b ,r and >.

Use the regex-based -split operator instead:

PS C:\> $String -split $separator |Where-Object {$_}
first line
second line 
 third line
 end

The Where-Object {$_} pipeline element will filter out empty strings

answered Jan 20, 2017 at 9:34
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.