Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Regular expression matching inner-most set of matched pairs.

$
0
0

I have a bit of XML that I search for matched pairs and then eventually replace the string with something else.

$msg = @'<table>  <tr>    <td>Album</td>    <td>Price</td>  </tr>  <tr>    <td>Archangel</td>    <td>$12</td>  </tr>  <tr>    <td>Stingray</td>    <td>$14</td>  </tr></table>
'@
while ( $msg -match '(?s)(?<tbl><table>.+?</table>)' ) {    # Do my thing... }
This works fine until I have nested pairs.

<table>  <tr>    <td>Album</td>    <td>Price</td>  </tr>  <tr>    <td>Archangel</td>    <td>      <table>        <tr>          <td>Format</td>          <td>Price</td>        </tr>        <tr>          <td>CD</td>          <td>$12</td>        </tr>        <tr>          <td>Vinyl</td>          <td>$14</td>        </tr>      </table>    </td>  </tr>  <tr>    <td>Stingray</td>    <td>$14</td>  </tr></table>

Now, if I could match just the inner most <table>, replace it and loop again, I'd have what I want but unfortunately I cannot find a means to match just the inner most <table> construct first.  I've read a number of articles on the web about balanced group matching in .NET regexp - and since I want to match the inner-most first I'm not even sure that's necessary - but in any event I'm just not getting it. Anyone have an idea how to do this?

Thanks,
Tim



Viewing all articles
Browse latest Browse all 21975

Trending Articles