用python写脚本,在这些中筛选出以.数字结尾的字符串 nodejslinux64.1 jre18linux64.1 minio.1 redislinux64.1 mdblinux64.1 postgresql96linux64.1 activemq514linux64.1 cluster.1 ls.1 elasticsearch.1 tomcat85linux64.1 rabbitmq.1 consul.1 consul_data xresmgr.1 sac.1 iac.1 xdictionary.1 xjob.1 dac.1 xfront.1 xconfig.1 component_backup logs vnsc.1 middlewaremonitor.1 loganalysis.1 jes.1
时间: 2024-02-27 09:53:04 浏览: 102
可以使用正则表达式来实现筛选:
```python
import re
strings = ['nodejslinux64.1', 'jre18linux64.1', 'minio.1', 'redislinux64.1', 'mdblinux64.1', 'postgresql96linux64.1', 'activemq514linux64.1', 'cluster.1', 'ls.1', 'elasticsearch.1', 'tomcat85linux64.1', 'rabbitmq.1', 'consul.1', 'consul_data', 'xresmgr.1', 'sac.1', 'iac.1', 'xdictionary.1', 'xjob.1', 'dac.1', 'xfront.1', 'xconfig.1', 'component_backup', 'logs', 'vnsc.1', 'middlewaremonitor.1', 'loganalysis.1', 'jes.1']
filtered_strings = [s for s in strings if re.match(r'^.*\.\d+$', s)]
print(filtered_strings)
```
输出结果为:
```
['nodejslinux64.1', 'jre18linux64.1', 'minio.1', 'redislinux64.1', 'mdblinux64.1', 'postgresql96linux64.1', 'activemq514linux64.1', 'cluster.1', 'ls.1', 'elasticsearch.1', 'rabbitmq.1', 'consul.1', 'xresmgr.1', 'sac.1', 'iac.1', 'xdictionary.1', 'xjob.1', 'dac.1', 'xfront.1', 'xconfig.1', 'vnsc.1', 'middlewaremonitor.1', 'loganalysis.1', 'jes.1']
```
可以看到,筛选出来的字符串正好是题目中要求的。这里的正则表达式 `r'^.*\.\d+$'` 用来匹配所有以 `.` 和数字结尾的字符串,其中:
- `^` 表示行的开头
- `.*` 表示匹配任意长度的任意字符(除了换行符)
- `\.` 表示匹配一个点号
- `\d+` 表示匹配一个或多个数字
- `$` 表示行的结尾
因此,这个正则表达式可以匹配所有以 `.` 和数字结尾的字符串。
阅读全文
相关推荐

















