![](/img/trans.png)
[英]How to search/find special characters like &, < or > in the string with regex using Python
[英]How to search for an exact match of special characters in a string using regex python
我目前正在使用 python 处理正则表达式。 我对 python 中的正则表达式很陌生。 作为我学习的一部分,我有以下问题
String1 = "[hello]"
Result = re.sub(re.compile("[.*?]>"),"", Result)
print(string)
现在我的Result
应该什么都不是
但是如果String1 = "[*hello]"
那么我的结果应该是[*hello]
帮我解决这个问题
很抱歉我在手机上输入错误评论并且不清楚地阅读了问题。 现在请允许我澄清:
您需要的正则表达式是:
^(\[\*)[^ ]*
你可以在Regex101上测试它,
如果字符串以 [* 开头,它将返回一个匹配项,并返回以下字符串的精确匹配,直到下一个空格,如果它是您想要的不同内容,请评论您的确切字符串结尾(如果存在)。
很简单,作为对你的字符串的测试:
my_str = '[hello] and [*hello]'
res = re.search('(\[\*\s?\w*\+?\w*)\]), my_str)#to also match closing bracket - (\[\*\w+)\]
print(res.group(0))
那只会输出[*hello]
因为它有很多变量..我建议你偏离正则表达式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.