.*(?<!\/)\/[^\/\s].*可以匹配的字符串有哪些
时间: 2023-07-22 11:18:43 浏览: 65
这个正则表达式可以匹配以下类型的字符串:
- 任意字符开头,包含斜杠,斜杠后跟着一个非斜杠、非空白字符的字符串,例如:`/abc`、`./test`、`?path`、`#hello/world` 等等。
- 任意字符开头,包含斜杠,斜杠后跟着一个问号和一个非斜杠、非空白字符的字符串,例如:`/abc?test`、`./test?query` 等等。
- 任意字符开头,包含多个斜杠,但是第一个斜杠后跟着一个非斜杠、非空白字符的字符串,例如:`/abc/def`、`./test/1234`、`#hello/world/123` 等等。
但是,以下类型的字符串无法匹配:
- 不包含斜杠的字符串,例如:`hello`、`123` 等等。
- 斜杠后面紧跟着空白字符的字符串,例如:`/ `、`/ abc` 等等。
- 以斜杠结尾的字符串,例如:`/`、`./` 等等。
相关问题
''' ''' import re from urllib.request import urlopen def getPage(url): # 获取网页的字符串 response = urlopen(url) return response.read().decode('utf-8') def parsePage(s): ret = com.finditer(s) # 从s这个网页源码中,找到所有符合com正则表达式的内容,并且以迭代器的形式返回 for i in ret: yield{ 'id':i.group('id'), 'title':i.group('title'), 'rating_num':i.group('rating_num'), 'comment_num':i.group('comment_num') } def main(num): # 翻页,执行10次 url = 'https://movie.douban.com/top250?start=%s&filter='% num response_html = getPage(url) # response_html就是这个url对应的html代码,就是 str ret = parsePage(response_html) print(ret) f = open('move_info7','a',encoding='utf8') for obj in ret: print(obj) data = str(obj) f.write(data +'\n') f.close() com = re.compile( '<div class = "item">.*?<div class="pic">.*?<em.*?>(?P<id>\d+).*?<span class = "title">(?P<title>.*?)</span>' '.*?<span class="rating_num".*?>(?P<rating_num>.*?)</span>.*?<span>(?P<comment_num>.*?)评价</span>',re.S) count = 0 for i in range(10): main(count) count += 25 检查这段代码并且进行修改
这段代码是用来爬取豆瓣电影Top250的信息,并且将结果保存到文件中。但是,代码中存在一些错误和需要改进的地方,建议进行如下修改:
1. 将文件名由'move_info7'改为'movie_info.txt',并且将文件打开操作放在for循环外面,避免重复打开和关闭文件。
2. 将函数名从'getPage'改为'get_page',符合Python的命名规范。
3. 将参数名从's'改为'html',更加直观。
4. 将正则表达式中的空格去掉,避免匹配不到结果。
5. 在文件写入操作之前,需要先检查文件是否存在,如果不存在,则需要创建文件。
修改后的代码如下:
```python
import re
from urllib.request import urlopen
def get_page(url):
# 获取网页的字符串
response = urlopen(url)
return response.read().decode('utf-8')
def parse_page(html):
com = re.compile(
'<div class="item">.*?<div class="pic">.*?<em.*?>(?P<id>\d+).*?<span class="title">(?P<title>.*?)</span>'
'.*?<span class="rating_num".*?>(?P<rating_num>.*?)</span>.*?<span>(?P<comment_num>.*?)评价</span>', re.S)
ret = com.finditer(html) # 从html这个网页源码中,找到所有符合com正则表达式的内容,并且以迭代器的形式返回
for i in ret:
yield {
'id': i.group('id'),
'title': i.group('title'),
'rating_num': i.group('rating_num'),
'comment_num': i.group('comment_num')
}
def main(num):
# 翻页,执行10次
url = 'https://movie.douban.com/top250?start=%s&filter=' % num
html = get_page(url) # html就是这个url对应的html代码,就是 str
ret = parse_page(html)
print(ret)
with open('movie_info.txt', 'a', encoding='utf8') as f:
for obj in ret:
print(obj)
data = str(obj)
f.write(data + '\n')
if __name__ == '__main__':
count = 0
for i in range(10):
main(count)
count += 25
```
/(?<=class="bild-titel">).*?(?=<\/div>)/s 改为python 正则
在 Python 中,正则表达式的使用和 PHP 略有不同。可以使用 `re` 模块进行正则表达式匹配。以下是将 `/(?<=class="bild-titel">).*?(?=<\/div>)/s` 正则表达式改写成 Python 正则表达式的示例代码:
```python
import re
pattern = r'(?<=class="bild-titel">).*?(?=<\/div>)s'
string = '<div class="bild-titel">发动机控制单元, 保险丝架 B, 散热器风扇</div>'
matches = re.findall(pattern, string)
print(matches[0])
```
输出结果为:
```
发动机控制单元, 保险丝架 B, 散热器风扇
```
需要注意的是,在 Python 中,正则表达式需要使用原始字符串,即在字符串前面加上 `r`,以防止反斜杠被转义。另外,`re.findall()` 函数可以返回所有匹配的结果,而不像 PHP 中的 `preg_match()` 函数只返回第一个匹配结果。