我正在尝试编写一些代码来查找for循环,用@符号替换它们包含的分号,并在右括号后添加一个新行。我目前的算法是
pattern = "for(";
if (line.contains(pattern))
{
openPos = line.indexOf(pattern) + "for".length();
Occurence = 1;
closePos = findClose(line, openPos, '(', ')');
if (closePos != -1)
{
// Replace all line terminators within loop ()'s with @'s
for (int lt = 0; lt < lineTerminator.size(); lt++)
{
tempLine = line.substring(openPos + "(".length(), closePos).replaceAll(";", "@");
}
line = line.substring(0, openPos + "(".length()) + tempLine + ")\n" + line.substring(closePos + 1, line.length()).trim();
multiLine = "";
}
}
这适用于一行for循环的单个实例,但我遇到的一个新案例是在生产JavaScript文件上运行它时,它在第一个之后的任何for循环上都不起作用。我试图在while循环中封装它以继续在同一行,同时它可以继续查找循环,如下所示
indexOfPattern = line.indexOf(pattern);
while (indexOfPattern >= 0)
{
openPos = indexOfPattern + pattern.length();
Occurence = 1;
closePos = findClose(line, openPos, '(', ')');
if (closePos != -1)
{
// Replace all line terminators within additional loop ()'s with @'s
for (int lt = 0; lt < lineTerminator.size(); lt++)
{
tempLine = line.substring(openPos + "(".length(), closePos).trim().replaceAll(lineTerminator.get(lt), "@");
}
line = line.substring(0, openPos + "(".length()) + tempLine + ")\n" + line.substring(closePos + 1, line.length()).trim();
}
indexOfPattern = line.indexOf(pattern, indexOfPattern + pattern.length());
}
但是这会替换for循环之外的分号。有谁知道这样做的任何更光滑的方式?
编辑:这是一些预期的输出
输入:
for(h=0;b[h];) for(i=0;i<10;i++) for(a in b) { do; some; things; }
输出:
for(h=0@b[h]@) for(i=0@i<10@i++) for(a in b) { do; some; things; }
编辑2:我选择了正则表达式的答案,因为它似乎适用于很多情况,除了这个(前面的荒谬的javascript垃圾):
for(b[this.id]=this,this.settings=new c.classes.configurable(c.settings,j.settings||{}),Object.defineProperty(this,"graph",{value:new c.classes.graph(this.settings),configurable:!0}),Object.defineProperty(this,"middlewares",{value:[],configurable:!0}),Object.defineProperty(this,"cameras",{value:{},configurable:!0}),Object.defineProperty(this,"renderers",{value:{},configurable:!0}),Object.defineProperty(this,"renderersPerCamera",{value:{},configurable:!0}),Object.defineProperty(this,"cameraFrames",{value:{},configurable:!0}),Object.defineProperty(this,"camera",{get:function(){return this.cameras[0]}}),Object.defineProperty(this,"events",{value:["click","rightClick","clickStage","doubleClickStage","rightClickStage","clickNode","clickNodes","doubleClickNode","doubleClickNodes","rightClickNode","rightClickNodes","overNode","overNodes","outNode","outNodes","downNode","downNodes","upNode","upNodes"],configurable:!0}),this._handler=function(a){var b,c={};for(b in a.data)c[b]=a.data[b];c.renderer=a.target,this.dispatchEvent(a.type,c)}.bind(this),f=j.renderers||[],d=0,e=f.length;e>d;d++)
注意嵌套的for(b in a.data)
到最后 - 这就是给出正则表达式答案问题的原因。是否有人能够处理这个愚蠢的案件?
答案 0 :(得分:1)
这是一种正则表达式方法......
public String replaceForSemicolons(String input) {
String pattern = "for\\s*\\([^;]+;[^;]+[^\\)]+\\)\\s*\\{";
Pattern reg = Pattern.compile(pattern);
Matcher matcher = reg.matcher(input);
StringBuffer output = new StringBuffer();
int previousEnd = 0;
while(matcher.find()) {
//get the matched 'for' without the opening bracket
String matchedString = input.substring(matcher.start(), matcher.end()-1);
//replace the semicolons with @
matchedString = matchedString.replaceAll(";", "@");
//append everything from the end of the last match to the start of this match
output.append(input.substring(previousEnd, matcher.start()));
//append the matched string with the replaced semicolons
output.append(matchedString);
//add a new line and the opening bracket that we left out from the matched string
output.append("\n{");
previousEnd = matcher.end();
}
//append the rest of the string
output.append(input.substring(previousEnd));
return output.toString();
}
答案 1 :(得分:0)
除非你使用某种类型的tokenization,否则即使不是完全不可能完成所有情况的100%也是如此。例如,如果您有以下内容:
for(b [this.id] = this,this.settings = new c.classes.configurable(c.settings,j.settings || {}), Object.defineProperty(this,&#34; graph&#34;,{
正则表达式会卡在{
的{{1}}上,而不是一直到j.settings || {}
这与can not really parse HTML or XML with regex
的原因相同。而不是进行搜索/替换,你真的需要构建一个简单的标记化器,例如看看下面的psudo代码:
d; d++)
你很可能不得不在上面的标记器中添加一些额外的状态来完成你想要做的所有事情......但是它应该给你一个好的起点。