鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Java/ 正則表達式抓取

正則表達式抓取

>>> str=''' <td>
...                                     
...                                         
...                                         應(yīng)用推廣
...                                     
...                                 </td>
...                                 <td>
...                                     
...                                         大圖廣告
...                                         
...                                         
...                                         
...                                     
...                                 </td>
...                                 <td>
...                                     信息流大圖D16
...                                 </td>'''


>>> s=re.search('<td>.*?</td>.*?<td>.*?</td>.*?<td>(.*?)</td>',str,re.S).group(1)
>>> a
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'a' is not defined
>>> s
'\n\t\t\t\t\t\t\t\t\t\xe4\xbf\xa1\xe6\x81\xaf\xe6\xb5\x81\xe5\xa4\xa7\xe5\x9b\xbeD16\n\t\t\t\t\t\t\t\t'
>>> s.strip(" ")
'\n\t\t\t\t\t\t\t\t\t\xe4\xbf\xa1\xe6\x81\xaf\xe6\xb5\x81\xe5\xa4\xa7\xe5\x9b\xbeD16\n\t\t\t\t\t\t\t\t'

正則如何匹配里面 ”信息流大圖D16”不要其他的空格t n ?

回答
編輯回答
你的瞳

re.search('(信)(.*?)(6)',str).group()

2017年8月9日 03:17
編輯回答
愚念

用re的sub替換就好

In [9]: a = u'ntttttttttxe4xbfxa1xe6x81xafxe6xb5x81xe5xa4xa7xe5x9bxbeD16ntttttttt'

In [10]: import re

In [11]: a1 = re.sub("\t","",a)

In [12]: a2 = re.sub("\n","",a1)

2017年3月26日 23:34