使用正则表达式来匹配带有连字符的文本。
代码示例:
import re
from bs4 import BeautifulSoup
html = """
This is a test-text with hyphenated words.
"""
soup = BeautifulSoup(html, 'html.parser')
p = soup.find('p')
text = p.get_text()
# 使用正则表达式匹配连字符
text_with_hyphens = re.findall(r'\b\w+-\w+\b', text)
print(text_with_hyphens) # 输出 ['hyphenated']