我需要一个正则表达式来选择两个外括号之间的所有文本.
例: some text(text here(possible text)text(possible text(more text)))end text
结果: (text here(possible text)text(possible text(more text)))
显然,这个问题在阅读之后会经常出现
并且思考了一会儿的问题,我写了一个函数来返回包含在任意数量的嵌套()内的内容
该函数可以很容易地扩展到任何正则表达式对象,在此发布您的想法和注意事项.
任何重构建议将不胜感激
(注意,我仍然是python的新手,并且不想弄清楚如何引发异常或其他什么,所以我只是让函数返回'失败',如果它不能弄清楚发生了什么)
编辑功能以考虑评论:
def ParseNestedParen(string, level):
"""
Return string contained in nested (), indexing i = level
"""
CountLeft = len(re.findall("\(", string))
CountRight = len(re.findall("\)", string))
if CountLeft == CountRight:
LeftRightIndex = [x for x in zip(
[Left.start()+1 for Left in re.finditer('\(', string)],
reversed([Right.start() for Right in re.finditer('\)', string)]))]
elif CountLeft > CountRight:
return ParseNestedParen(string + ')', level)
elif CountLeft < CountRight:
return ParseNestedParen('(' + string, level)
return string[LeftRightIndex[level][0]:LeftRightIndex[level][1]]
Run Code Online (Sandbox Code Playgroud) 我正在使用python正则表达式模块,re.
我需要在这两个短语中匹配'('')'内的任何内容,但"不那么贪心".像这样:
show the (name) of the (person)
calc the sqrt of (+ (* (2 4) 3))
Run Code Online (Sandbox Code Playgroud)
结果应该从短语1返回:
name
person
Run Code Online (Sandbox Code Playgroud)
结果应该从短语2返回:
+ (* (2 4) 3)
Run Code Online (Sandbox Code Playgroud)
问题是,为了适应第一个短语,我使用了 '\(.*?\)'
在第二个短语中,这恰好适合 + (* (2 4)
并使用'\(.*\)'正确拟合第二个短语,在第一个短语适合(name) of the (person)
什么正则表达式适用于这两个短语?
我正在寻找一个正则表达式来替换括号中的所有项,除非括号在方括号内.
例如
(matches) #match
[(do not match)] #should not match
[[does (not match)]] #should not match
Run Code Online (Sandbox Code Playgroud)
我目前有:
[^\]]\([^()]*\) #Not a square bracket, an opening bracket, any non-bracket character and a closing bracket.
Run Code Online (Sandbox Code Playgroud)
但是,这仍然是方括号内的匹配单词.
到目前为止,我还创建了一个我的进度的rubular页面:http://rubular.com/r/gG22pFk2Ld
所以我需要编写一个脚本,最大的问题之一归结为找到字符串中最大的有效子序列。所以我有类似的东西
"()(({}[](][{[()]}]{})))("
Run Code Online (Sandbox Code Playgroud)
作为输入,我需要返回
"[{[()]}]{}"
Run Code Online (Sandbox Code Playgroud)
作为输出。
我尝试过使用类似堆栈的结构,就像您在只是括号时所做的那样,但无法找出有效的方法。我更喜欢 python 中的解决方案,但任何人都可以提供的任何指导都会有所帮助,无论语言如何。理想情况下,效率应该比 n^2 更好,因为我可以使用这个How to find valid of a string of括号、大括号和方括号?想到 O(n^2) 解决方案。只是在不同的子字符串上尝试
这意味着我只想剥去封闭的大括号.我可以将"{this stuff of stuff}"与:
"{stuff}".match(/{([^}]*)}/)[1]
Run Code Online (Sandbox Code Playgroud)
我在这里问得太多了吗?
另一个例子,我把这个javascript代码作为字符串:
{
var foo = {
bar: 1
};
var foo2 = {
bar: 2
};
}
Run Code Online (Sandbox Code Playgroud)
我想只剥掉外面的大括号:
var foo = {
bar: 1
};
var foo2 = {
bar: 2
}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串
pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)
Run Code Online (Sandbox Code Playgroud)
我需要得到它
pow(sin(4*x+5),2)+pow(cos(4*x+3),3)/pow(tan(x),5)
Run Code Online (Sandbox Code Playgroud)
我试过的是
1)将基于运算符(+, - ,/,*)的表达式在pow之间拆分为单个单元.
2)提取最后一个parenthisis之间的表达
3)将提取的子表达式插入pow之后的第一个字符串和第一个关闭所有单位的parenthis之间.
我试过的: -
re="pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)+pow((tan),(5))(x)";
re.split(+);
re.replaceAll("^[()]]{3}","\\(\\)]*\\)");
Run Code Online (Sandbox Code Playgroud)
坦率地说,我是正则表达的新手.
这个正则表达式
/\(.*\)/
Run Code Online (Sandbox Code Playgroud)
将不匹配匹配的括号,但匹配字符串中的最后一个括号.是否有正则表达式扩展或类似的东西,具有适当的语法允许这个?例如:
there are (many (things (on) the)) box (except (carrots (and apples)))
Run Code Online (Sandbox Code Playgroud)
/OPEN(.*CLOSE)/ 应该匹配 (many (things (on) the))
可能有无限级别的括号.
regex ×6
python ×3
javascript ×2
parentheses ×2
parsing ×2
algorithm ×1
regex-greedy ×1
ruby ×1
stack ×1
string ×1