使用python regex

时间:2017-09-25 08:07:34

标签: regex python-2.7

我想在一个文件中跟随字符串模式,我想用另一个字符串替换它

import re
stringPatterenInFile = """ <div id="A4">

<br/>

<div style="padding: 2%;text-align: left;">

<p ><span style="color: #403052;"> <span style="font-size: large;"><strong>COMMERCIALS </strong></span></span><span style="color: #403052;"><span style="font-size: large;">AND TERMS OF ENGAGEMENT:<br /></span></span></p>
<p  align="justify">&nbsp;</p>



<div id="dynamicTable1">

<table class="dynamicTable">
  <tr>
    <th>Description</th>
    <th>Price</th> 
    <th>Quantity</th>
    <th>Quote</th>
    <th>Tax</th>
    <th>Total</th>
  </tr>

  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  </tr>
  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  </tr>
  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  </tr>
  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  </tr>
  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  </tr>
  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  </tr>
  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  <tr>
    <td>Earthing Cabling</td>
    <td>100</td>   
    <td>345</td>
    <td>34500</td>
    <td>5175</td>
    <td>39675</td>

  </tr>
</table>
</div>

</div>
</div>

<br/>"""

想要替换为: -

<div id='dynamicTable1'><table class='dynamicTable'><tr><th>Description</th><th>Price</th><th>Quantity</th><th>Quote</th><th>Tax</th><th>Total</<tr><td>Earthing Cabling</td><td>100</td><td>345</td><td>34500</td><td>5175</td><td>39675</td></tr></table></div>

我正在使用正则表达式: -

a = re.match(r"^<div id='dynamicTable1'><table class='dynamicTable'>(.*?)</table></div></div></div>$", text)

但它正在打印None

1 个答案:

答案 0 :(得分:0)

我得到的解决方案如下:

<div id="MainNavContainer">
  <ul class="MainNav">
    <li>
      <a href="#">
        <span class="NavLine"></span>
        <span class="NavTxt">HOME</span>
      </a>
    </li>
    <li>
      <a href="#">
        <span class="NavLine"></span>
        <span class="NavTxt">SERVICES</span>
      </a>
    </li>
  </ul>
</div>