相关疑难解决方法(0)

如何忽略正则表达式主题字符串中的空格?

在使用正则表达式模式搜索匹配项时,是否有一种简单的方法可以忽略目标字符串中的空格?例如,如果我的搜索是"猫",我希望"c ats"或"ca ts"匹配.我不能事先删除空格,因为我需要找到匹配的开始和结束索引(包括任何空格)以突出显示该匹配,并且任何空格都需要用于格式化目的.

regex whitespace

95
推荐指数
4
解决办法
19万
查看次数

如何在python beautifulsoup中加入工作

我正在学习python和beautifulsoup,并在网上看到了这段代码:

from BeautifulSoup import BeautifulSoup, SoupStrainer
import re

html = ['<html><body><p align="center"><b><font size="2">Table 1</font></b><table><tr><td>1. row 1, cell 1</td><td>1. row 1, cell 2</td></tr><tr><td>1. row 2, cell 1</td><td>1. row 2, cell 2</td></tr></table><p align="center"><b><font size="2">Table 2</font></b><table><tr><td>2. row 1, cell 1</td><td>2. row 1, cell 2</td></tr><tr><td>2. row 2, cell 1</td><td>2. row 2, cell 2</td></tr></table></html>']
soup = BeautifulSoup(''.join(html))
searchtext = re.compile(r'Table\s+1',re.IGNORECASE)
foundtext = soup.find('p',text=searchtext) # Find the first <p> tag with the search text
table = foundtext.findNext('table') # Find the first <table> tag that follows it …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

2
推荐指数
1
解决办法
3221
查看次数

标签 统计

beautifulsoup ×1

python ×1

regex ×1

whitespace ×1