简体   繁体   English

从字符串中提取括号之间的字符串

[英]Extract Strings between parenthese from String

I am working on my own little Script Language. 我正在使用自己的小脚本语言。 Now I am stucking at the Script Reader which reads the file and converts it into the Script Parts (variables, functions, if clauses etc). 现在,我停留在脚本读取器上,该脚本读取器读取文件并将其转换为脚本部分(变量,函数,if子句等)。 I hava String which saves the Script File content in one line like so: 我拥有将脚本文件内容保存在一行中的String,如下所示:

mega = "";test(){hallo();test = "";if(){p2 = -1;}else{p1 = "";}hi = 0;}

Now my Problem. 现在是我的问题。 I want to get the strings between the parentheses for if clauses and the function (which is around the if clause). 我想获取if子句和函数(位于if子句周围)之间的字符串。 I tried it with regex: 我用正则表达式尝试过:

Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(example);
 while(m.find()) {
   System.out.println(m.group(1));    
 }

But then I get the content between the first parenthese of the function test(){ and the first parenthese that clauses the if clause. 但是然后我得到函数test(){的第一个括号和带有if子句的第一个括号之间的内容。 How can I extract the Strings between the if clause, else clause and the function? 如何提取if子句,else子句和函数之间的字符串?

EDIT: 编辑:

This is want I want to get out after the extraction: 这是我想在提取后下车的方法:

For the If: p2 = -1; 对于If: p2 = -1; For the Else: p1 = ""; 对于其他: p1 = ""; And for the Function arround: 对于功能范围:

hallo();test = "";if(){p2 = -1;}else{p1 = "";}hi = 0;

EDIT 2: I would like to have it recursive to its endless (when in the if clause is another etc.) 编辑2:我想让它递归到无尽的(当if子句中是另一个时)

I think that regular expressions will not do that. 我认为正则表达式不会做到这一点。 You are looking after correspondent pairs of curly brackets. 您正在寻找对应的大括号对。 Reminds me of RegEx match open tags except XHTML self-contained tags . 让我想起了RegEx匹配开放标签,除了XHTML自包含标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM