[英]Getting content of function (Between curly braces)
I have a class with "n" complex functions like this: 我有一个带有“ n”个复杂函数的类,如下所示:
public class Foo {
public String getSmt(String param, Foo foo) {
try {
if (smtIsTrue()) {
Thread mThread = new Thread(new Runnable(){
@Override
public void run() {
// Here I do something
}
}).start();
} else if (other()) {
if (isExisted()) {
execute();
}
}
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
doSmt();
}
}
public String otherGetSmt(String param, Foo foo) {
try {
if (smtIsTrue()) {
Thread mThread = new Thread(new Runnable(){
@Override
public void run() {
// Here I do something
}
}).start();
} else if (other()) {
if (isExisted()) {
execute();
}
}
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
doSmt();
}
}
}
I would like to get content of two function by using Regex . 我想使用Regex获取两个函数的内容。 Could you so me how? 你能这样我吗? I tried this 我试过了
(public|protected|private|static|\s) +[\w\<\>\[\], ]+\s+(\w+) *\([^\)]*\) *(\{?|[^;]) .*\{((\s|.)*)\}
But I doesn't work fine. 但是我做得不好。
.
does not matches end line so it wont help. 与结束线不匹配,因此无济于事。
try this: [^{]*\\{([^aa]*)\\}
试试这个: [^{]*\\{([^aa]*)\\}
{(?:(?!\n{2,})[\s\S])*}
You can simply do this to get what you want.See demo. 您可以简单地执行此操作以获取所需内容。请参阅演示。
https://regex101.com/r/rU8yP6/14 https://regex101.com/r/rU8yP6/14
This regular expression will work: 此正则表达式将起作用:
.*\(.*\)\s*\{((\s|.)*)\}
You need to the get the second capturing group, ie, .*\\(.*\\)\\s*\\{((\\s|.)*)\\}
. 您需要获取第二个捕获组,即.*\\(.*\\)\\s*\\{((\\s|.)*)\\}
。 However, the content will be string. 但是,内容将是字符串。 If you want structured content, then you need to use a parser for Java. 如果需要结构化内容,则需要使用Java解析器。 You can take a look at Java Compiler Tree API, or use a standalone parser generator to generate a Java parser. 您可以查看Java编译器树API,或使用独立的解析器生成器来生成Java解析器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.