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)写出正确的正则表达式提取所有符合特定模式的内容。
时间: 2023-06-22 15:40:55 浏览: 213
正则表达式:ftp:\/\/\S+?\.(gz|xz)
解释:
- ftp:\/\/:匹配以ftp://开头的字符串
- \S+?\:匹配任意非空字符(非贪婪匹配)
- \.(gz|xz):匹配以.gz或.xz结尾的文件名
完整代码:
```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:\/\/\S+?\.(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']
```
阅读全文