Powershell + Regular Expressions - How to get multiple matches?
I have already broken my head trying to solve the problem below and I will
appreciate every comment or piece of advise on this.
Prerequisites
1) HTML text
<div style="font-size:8pt; font-family: Calibri, sans-serif;">Some text
here</div>
2) Powershell v.3
Task
To parse given text and select only tags
Approach
$text_to_parse = '<div style="font-size:8pt; font-family: Calibri,
sans-serif;">Some text here</div>'
if($text_to_parse -match '</?div[^<>]*>'){$Matches | fl}
Name : 0
Value : <div style="font-size:8pt; font-family: Calibri, sans-serif;">
Issues
1) As you can see, it does not show second match despite /?quantifier 2) I
do understand, that there must be "Global" anchor, but I cannot find it
even in MSDN: http://msdn.microsoft.com/library/az24scfc.aspx 3) \G anchor
doesn't work as well even if I add pattern for one or more character in
the begining:
if($text_to_parse -match '\G<.*?/?div[^<>]*>'){$Matches | fl}
Name : 0
Value : <div style="font-size:8pt; font-family: Calibri, sans-serif;">`
Questions
1) What I am doing wrong? I spent well more 4 hours trying to figure it
out without any success. 2) Is there any "Global" anchor in RegEx
realization in Powershell? 3) Finally, how to match both HTML tags with
Regular Expressions only? I can do something like this:
($text_to_parse -replace '\G<.*?/?div[^<>]*>',"").TrimEnd("</div>")
And get this:
Some text here
But I'd like to do this with Regular Expressions.
Kind regards, Yuriy
No comments:
Post a Comment