[英]Regex : Find string between brackets after a string
So far my regex is ->到目前为止,我的正则表达式是 ->
myRegex = /catch *\(.\) */
I want to be able to search the following string -我希望能够搜索以下字符串 -
try {
print(new Function (astatement)())
} catch (e) { print(e.stack) }
So that if.stack is present after catch(anything) it should return true, so far I have managed to reach the catch(anything), stuck from here.因此 if.stack 在 catch(anything) 之后出现,它应该返回 true,到目前为止我已经设法到达 catch(anything),从这里卡住。
Matching simple code in the exception handler, provided there are no nested curly braces:匹配异常处理程序中的简单代码,前提是没有嵌套的花括号:
catch *\((\w+)\) *{[^}]+(\1)(\.stack)[^}]+}
The above regular expression matches these examples上面的正则表达式匹配这些例子
(capturing groups for my sake, remove them if not needed): (为了我的缘故捕获组,如果不需要则删除它们):
try {
print(new Function (astatement)())
} catch (e) { print(e.stack) }
try {
print(new Function (bstatement)())
} catch (exception) { print(exception.stack) }
You can try it out here:你可以在这里试试:
https://regex101.com/r/PuytDJ/2 https://regex101.com/r/PuytDJ/2
\w+
Instead of just .
而不仅仅是
.
I assume you can give the exception instance any name, so maybe more safe to use \w+
here.我假设你可以给异常实例任何名称,所以在这里使用
\w+
可能更安全。
{[^}]+
This looks for an opening curly brace and catches every non-closing curly brace until the next expression.这将查找左大括号并捕获每个非右大括号直到下一个表达式。 It works only if you do not have nested curly braces in your exception handler.
它仅在您的异常处理程序中没有嵌套花括号时才有效。
(\1)
Here I reference the exception's variable name found in capturing group 1.在这里,我引用了在捕获组 1 中找到的异常变量名称。
(\.stack)
Here is finding .stack
literally这是从字面上找到
.stack
[^}]+}
I continue to parse for all non-curly braces characters and finally the closing curly bracket.我继续解析所有非大括号字符,最后是右花括号。
⚠ This being said, regular expressions cannot parse nested elements very well, or only to a certain degree with increasingly more complex expressions. ⚠ 也就是说,正则表达式不能很好地解析嵌套元素,或者只能在一定程度上解析越来越复杂的表达式。
So code blocks, HTML/XML Tags, JSON objects etc. are better parsed, not text-searched in.所以代码块、HTML/XML 标签、JSON 对象等被更好地解析,而不是文本搜索。
Using a Javascript parser, you can traverse through the syntax-tree (AST) and find each ".stack" inside a catch-block.使用 Javascript 解析器,您可以遍历语法树 (AST) 并在 catch 块中找到每个“.stack”。
For example use slimit
in Python:例如在 Python 中使用
slimit
:
from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor
data = """
try {
print(new Function(astatement)())
} catch (e) {
print(e.stack)
}
"""
parser = Parser()
tree = parser.parse(data)
def nextCatch(tree):
for node in nodevisitor.visit(tree):
if isinstance(node, ast.Catch):
return node
def nextIdentifierStack(tree):
for node in nodevisitor.visit(tree):
if isinstance(node, ast.Identifier) and node.value == 'stack':
return node
def printChildren(node):
for i, ch in enumerate(node.children()):
print(f"{i:2}: {ch.to_ecma()}")
catch = nextCatch(tree)
printChildren(catch)
stack = nextIdentifierStack(catch)
print(stack.to_ecma())
Prints: the catch block with 2 children and the found stack
identifier打印:带有 2 个孩子的 catch 块和找到的
stack
标识符
0: e
1: {
print(e.stack);
}
stack
See also JavaScript parser in Python另见Python 中的 JavaScript 解析器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.