gpt4 book ai didi

python - 如何在Python中的单个正则表达式中搜索正则表达式组中的特定字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:44 24 4
gpt4 key购买 nike

我正在尝试将“名词”一词与我的正则表达式最后一组中的单词进行匹配。

到目前为止我已经:

tags = 'motocykl mutka 1 motorcycle bike moped 0 transportation openair noun'

print re.search('(?P<pol>\D+)(?P<d1>\d)(?P<eng>\D+)(?P<d2>\d)(?P<end>\D+)', tags).group('end')

我得到的只是一个字符串,它是最后一组:

transportation openair noun

我需要得到:

noun

更新:

我忘了提及,“名词”不会显示为我将运行正则表达式的某些字符串中的最后单词。例如:

tags = 'dźwig 1 crane 0 noun construction vehicle'
tags = 'trycykl 1 tricycle 0 child noun transportation'

关于如何在单个正则表达式中执行此操作有什么想法吗?

最佳答案

不确定你的标签是什么意思,但是\D+?应匹配“transport openair”和 [a-zA-Z]将匹配最后一个单词(名词):

^(?P<pol>\D+)(?P<d1>\d)(?P<eng>\D+)(?P<d2>\d)\D+?(?P<end>[a-zA-Z]+)$

关于python - 如何在Python中的单个正则表达式中搜索正则表达式组中的特定字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7458669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com