正则表达式返回两个特殊字符之间的所有字符 [英] Regular expression to return all characters between two special characters

查看:154
本文介绍了正则表达式返回两个特殊字符之间的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用regx返回两个方括号之间的所有字符. 这是一个示例:

How would I go about using regx to return all characters between two brackets. Here is an example:

foobar['infoNeededHere']ddd
needs to return infoNeededHere

我发现一个正则表达式可以在花括号之间进行操作,但是所有尝试使其与方括号一起使用的尝试均失败了.这是该正则表达式:(?<={)[^}]*(?=}),这是我尝试对其进行破解

I found a regex to do it between curly brackets but all attempts at making it work with square brackets have failed. Here is that regex: (?<={)[^}]*(?=}) and here is my attempt to hack it

(?<=[)[^}]*(?=])

最终解决方案:

import re

str = "foobar['InfoNeeded'],"
match = re.match(r"^.*\['(.*)'\].*$",str)
print match.group(1)

推荐答案

^.*\['(.*)'\].*$ 将与排成一行并捕获您想要的东西.

^.*\['(.*)'\].*$ will match a line and capture what you want in a group.

您必须使用\

rubular.com 证明链接上的文档将解释表达式的形成方式.

The documentation at the rubular.com proof link will explain how the expression is formed.

这篇关于正则表达式返回两个特殊字符之间的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆