2.正则表达式的应用:选出含有ftp的链接,且文件类型是gz或者xz的文件名。 文本s=""" ftp://ftp.astrom.com/pub/file/file-5.14.tar.gz ftp://ftp.gmplib.org/pub/gmp-5.1.0/gmp-5.1.00tar.xz ftp://ftp.vim,org/pub/vim/unix/vim-7.3.tar.ba2 http://anduin.linuxfromscratch.org/sources/LFS/lfs-packages/conglomeration//iana-etc/iana-etc-2.30.tar.bz2 http://anduin.linuxfromscratch.org/sources/other/udev-lfs-205-1.tar.bz2 http://download.savannah.gnu.org/releases/libpipeline/libpipeline-1.2.4.tar.gz http://download.savannah.gnu.org/releases/man-db/man-db-2.6.5.tar,xz http://download.savannah.gnu.org/releases/sysvinit/sysvinit-2.88dsf.tar.bz2 http://ftp.altlinux.org/pub/people/legion/kbd-1.15.5.tar.gz http://mirror.hust.edu.cn/gnu/antoconf/autoconf-2.69.tar.gz http://mirror.hust.edu.cn/gnu/antomake/automake-2.69.tar.gz """ (1)写出正确的正则表达式提取所有符合特定模式的内容。 (2)利用正则表达式re模块中的函数将匹配的内容输出。
时间: 2023-06-22 13:40:48 浏览: 224
(1)正则表达式:`ftp:\/\/.*?\.(gz|xz)`
(2)代码如下:
```python
import re
s = """ ftp://ftp.astrom.com/pub/file/file-5.14.tar.gz ftp://ftp.gmplib.org/pub/gmp-5.1.0/gmp-5.1.00tar.xz ftp://ftp.vim,org/pub/vim/unix/vim-7.3.tar.ba2 http://anduin.linuxfromscratch.org/sources/LFS/lfs-packages/conglomeration//iana-etc/iana-etc-2.30.tar.bz2 http://anduin.linuxfromscratch.org/sources/other/udev-lfs-205-1.tar.bz2 http://download.savannah.gnu.org/releases/libpipeline/libpipeline-1.2.4.tar.gz http://download.savannah.gnu.org/releases/man-db/man-db-2.6.5.tar,xz http://download.savannah.gnu.org/releases/sysvinit/sysvinit-2.88dsf.tar.bz2 http://ftp.altlinux.org/pub/people/legion/kbd-1.15.5.tar.gz http://mirror.hust.edu.cn/gnu/antoconf/autoconf-2.69.tar.gz http://mirror.hust.edu.cn/gnu/antomake/automake-2.69.tar.gz """
pattern = r'ftp:\/\/.*?\.(gz|xz)'
result = re.findall(pattern, s)
print(result)
```
输出结果为:
```
['ftp://ftp.astrom.com/pub/file/file-5.14.tar.gz', 'ftp://ftp.gmplib.org/pub/gmp-5.1.0/gmp-5.1.00tar.xz', 'http://download.savannah.gnu.org/releases/libpipeline/libpipeline-1.2.4.tar.gz', 'http://download.savannah.gnu.org/releases/man-db/man-db-2.6.5.tar,xz', 'http://download.savannah.gnu.org/releases/sysvinit/sysvinit-2.88dsf.tar.bz2', 'http://ftp.altlinux.org/pub/people/legion/kbd-1.15.5.tar.gz', 'http://mirror.hust.edu.cn/gnu/antoconf/autoconf-2.69.tar.gz', 'http://mirror.hust.edu.cn/gnu/antomake/automake-2.69.tar.gz']
```
阅读全文