使用Python 一个正则表达式匹配commit id: commit message: author 三个参数 带匹配内容为一个html文件
时间: 2024-02-20 11:58:54 浏览: 127
以下是使用Python正则表达式实现的代码示例:
```python
import re
# 匹配 commit id、commit message 和 author 的正则表达式
commit_id_pattern = r'commit id:(\w+)'
commit_message_pattern = r'commit message:(.*)'
author_pattern = r'author:(.*)'
# 匹配 HTML 标签的正则表达式
html_tag_pattern = r'<.*?>'
# 将 HTML 文件读入字符串
with open('file.html', 'r') as f:
html_string = f.read()
# 去除 HTML 标签
html_string = re.sub(html_tag_pattern, '', html_string)
# 匹配 commit id
commit_id_match = re.search(commit_id_pattern, html_string)
commit_id = commit_id_match.group(1)
# 匹配 commit message
commit_message_match = re.search(commit_message_pattern, html_string)
commit_message = commit_message_match.group(1)
# 匹配 author
author_match = re.search(author_pattern, html_string)
author = author_match.group(1)
print('commit id:', commit_id)
print('commit message:', commit_message)
print('author:', author)
```
需要将 `file.html` 替换成实际的 HTML 文件路径。如果 HTML 文件比较大,可以考虑使用流式读取。
阅读全文