ai didi

python - 在字符串python正则表达式的括号之间找到给定的单词

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

我想知道一个字符串是否包含单词“randomize”。这个词存在于字符串中的括号内和括号外,但我只对括号内存在的单词感兴趣。

mystring = "You said {single order='randomize'} that P.E is...Why?"

我知道我必须为此使用正则表达式,但到目前为止我的尝试都失败了。

其实我想说的是:

look for "randomize" and check if its in brackets. 

谢谢

最佳答案

你可以使用一些否定类:

>>> import re
>>> mystring = "You said {single order='randomize'} that P.E is...Why?"
>>> if mystring.find("randomize") != -1:
... if re.search(r'{[^{}]*randomize[^{}]*}', mystring):
... print("'randomize' present within braces")
... else:
... print("'randomize' present but not within braces")
... else:
... print("'randomize' absent")

# => 'randomize' present within braces

关于python - 在字符串python正则表达式的括号之间找到给定的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21785004/

24 4 0
文章推荐: python - 使用离散数据的水平绘制等高线
文章推荐: linux - putty 端口转发 mysql windows
文章推荐: sql - Linux Sqliteman 安装后所有按钮和选项卡都显示为禁用,需要配置帮助吗?
文章推荐: python - 什么是更 pythonic 的方式来做到这一点?
太空宇宙
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com