图文精华

分享

写一个匹配"_rt"和“_ro”结尾的正则表达式

小不点 发表于 2023-9-22 15:58:40 [显示全部楼层] 回帖奖励 阅读模式 2 152
介绍详细一些

已有(2)人评论

跳转到指定楼层

0

主题

0

回帖

-7

积分

站内AI

积分
-7
ChatGPT 发表于 2023-9-22 15:59:08

要匹配以"_rt"或"_ro"结尾的字符串,可以使用以下正则表达式:

.*(_rt|_ro)$

其中,"."表示匹配任意字符,"*"表示匹配前面的字符0次或多次,"|"表示或的关系,"$"表示匹配字符串结尾。

例如,可以使用以下代码在Python中使用该正则表达式进行匹配:

import re

strings = ["abc_rt", "def_ro", "ghi_rtj", "klm_op"]
pattern = re.compile(r".*(_rt|_ro)$")

for string in strings:
    if pattern.match(string):
        print(string, "matches the pattern")
    else:
        print(string, "does not match the pattern")

输出结果为:

abc_rt matches the pattern
def_ro matches the pattern
ghi_rtj does not match the pattern
klm_op does not match the pattern

这说明,只有以"_rt"或"_ro"结尾的字符串匹配成功,其他的字符串都不匹配。

1

主题

1

回帖

4

积分

新手上路

积分
4
小不点 发表于 2023-9-22 16:00:09
ChatGPT 发表于 2023-9-22 15:59
要匹配以"_rt"或"_ro"结尾的字符串,可以使用以下正则表达式:
.*(_rt|_ro)$

牛逼,真的可以呀
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则