正则表达式<span class="">(.+)</span><i>(.+)</i></div>匹配<span class="">58</span><i>万</i></div>
时间: 2023-11-02 17:59:23 浏览: 34
这个正则表达式可以匹配类似于以下格式的文本:
```html
<span class="">58</span><i>万</i></div>
```
其中,`<span>`标签中包含了数字部分,`<i>`标签中包含了单位部分。使用正则表达式可以方便地从这样的文本中提取数字和单位信息。
具体解释:
- `<span class="">` 匹配`<span>`标签,其中`class`属性为空。
- `(.+)` 匹配`<span>`标签中的任意内容,并捕获为第一个分组。
- `</span><i>` 匹配`</span><i>`这个固定的字符串。
- `(.+)` 匹配`<i>`标签中的任意内容,并捕获为第二个分组。
- `</i></div>` 匹配`</i></div>`这个固定的字符串。
其中,括号中的内容表示一个分组,可以在匹配过程中被捕获并使用。
相关问题
<div class="totalPrice totalPrice2"><i>参考价: </i><span class="">395</span><i>万</i></div>如何只取395
您可以使用字符串切片或正则表达式来提取数字部分。以下是字符串切片的示例代码:
```python
price_str = '<div class="totalPrice totalPrice2"><i>参考价: </i><span class="">395</span><i>万</i></div>'
start_index = price_str.index('">') + 2
end_index = price_str.index('</span>')
price = price_str[start_index:end_index]
print(price)
```
输出结果为:
```
395
```
以下是正则表达式的示例代码:
```python
import re
price_str = '<div class="totalPrice totalPrice2"><i>参考价: </i><span class="">395</span><i>万</i></div>'
pattern = re.compile(r'<span.*?>(.*?)</span>')
match = pattern.search(price_str)
if match:
price = match.group(1)
print(price)
```
输出结果为:
```
395
```
<li class="col-md-6 col-sm-4 col-xs-3"> <a class="myui-vodlist__thumb lazyload" href="/yhdm/1.html" title="海贼王" data-original="/uploads/allimg/202107/1e78c58b1cfef8fe.jpg"> <span class="play hidden-xs"></span> <span class="pic-tag pic-tag-top"> 4.0分 </span> <span class="pic-text text-right"> 更新至1063集 </span> </a> <div class="myui-vodlist__detail"> <h4 class="title text-overflow"><a href="/yhdm/1.html" title="海贼王">海贼王</a></h4> </div> </li>用正则表达式提取文字和数字
可以使用如下正则表达式提取文字和数字:
```python
import re
text = '<li class="col-md-6 col-sm-4 col-xs-3"> <a class="myui-vodlist__thumb lazyload" href="/yhdm/1.html" title="海贼王" data-original="/uploads/allimg/202107/1e78c58b1cfef8fe.jpg"> <span class="play hidden-xs"></span> <span class="pic-tag pic-tag-top"> 4.0分 </span> <span class="pic-text text-right"> 更新至1063集 </span> </a> <div class="myui-vodlist__detail"> <h4 class="title text-overflow"><a href="/yhdm/1.html" title="海贼王">海贼王</a></h4> </div> </li>'
pattern = r'<li class=".*?"><a.*? title="(.*?)" data-original="(.*?)">.*?([\d\.]+)分.*?更新至(\d+)集.*?</a>.*?</li>'
result = re.findall(pattern, text)
print(result)
```
输出结果为:
```python
[('海贼王', '/uploads/allimg/202107/1e78c58b1cfef8fe.jpg', '4.0', '1063')]
```
其中,元组中的第一个值为文字“海贼王”,第三个值为数字“4.0”,第四个值为数字“1063”。