Java 在单行上解析块注释
我想忽略/瞄准线的非块注释段 例如,以下字符串需要全部生成一个字符串Java 在单行上解析块注释,java,regex,string,comments,Java,Regex,String,Comments,我想忽略/瞄准线的非块注释段 例如,以下字符串需要全部生成一个字符串“foobar” 实现此功能的最佳方法是什么?编辑请尝试以下方法: public static void main(String[] args) { String[] input = new String[]{"foo/*comment*/bar", "comm*/foobar/*ent", "comment*/foobar", "foobar/*comment"}; String pattern = "(?:
“foobar”
实现此功能的最佳方法是什么?编辑请尝试以下方法:
public static void main(String[] args) {
String[] input = new String[]{"foo/*comment*/bar", "comm*/foobar/*ent", "comment*/foobar", "foobar/*comment"};
String pattern = "(?:/\\*[^\\*]+(?:\\*/)?|(?:/\\*)?[^\\*]+\\*/)";
List<String> listMatches = new ArrayList<String>();
String result = "";
for (String m : input) {
result = m.replaceAll(pattern, ""); //remove matches
listMatches.add(result); // append to list
System.out.println(result);
}
}
以下是正则表达式的解释:
(?: 1st non-capturing group starts
/\\* match /* literally
[^\\*]+ 1 or more times characters except *
(?: 2nd non-capturing group starts
\\*/ match */ literally
) 2nd non-capturing group ends
? match previous non-capturing group 0 or 1 time
| Or (signals next alternative)
(?: 3rd non-capturing group starts
/\\* match /* literally
) 3rd non-capturing group ends
? match previous non-capturing group 0 or 1 time
[^\\*]+ 1 or more times characters except *
\\*/ match */ one time
) 1st non-capturing group ends
这与post in具有相同的逻辑,但以递归形式实现,以满足您对简单性的需求:
public static String cleanComment(String str) {
int open = str.indexOf("/*"), close = str.indexOf("*/");
if( (open&close) < 0 ) return str;
open &= Integer.MAX_VALUE;
close &= Integer.MAX_VALUE;
if(open < close) {
if(close > str.length()) {
return str.substring(0, open);
} else {
return str.substring(0, open) + cleanComment( str.substring(close+2) );
}
} else {
return cleanComment( str.substring(close+2) );
}
}
publicstaticstringcleancoment(stringstr){
int open=str.indexOf(“/”),close=str.indexOf(“*/”);
如果((打开和关闭)<0)返回str;
打开&=Integer.MAX_值;
关闭&=Integer.MAX_值;
如果(打开<关闭){
如果(关闭>str.length()){
返回str.substring(0,打开);
}否则{
返回str.substring(0,打开)+cleanComment(str.substring(关闭+2));
}
}否则{
返回cleanComment(str.substring(close+2));
}
}
看看这个解决方案似乎相当复杂。我正试图找出最简单的解决办法。这个问题怎么会太宽泛了。这是一个目标明确的简单问题。我希望这些内容不在注释中。@Ogen:由于正则表达式正在查找所有注释,我们只需要删除所有匹配项。这实际上很有效。你能解释一下正则表达式的不同部分在做什么吗?上面的解释对你有好处吗?是的,很好,谢谢
(?: 1st non-capturing group starts
/\\* match /* literally
[^\\*]+ 1 or more times characters except *
(?: 2nd non-capturing group starts
\\*/ match */ literally
) 2nd non-capturing group ends
? match previous non-capturing group 0 or 1 time
| Or (signals next alternative)
(?: 3rd non-capturing group starts
/\\* match /* literally
) 3rd non-capturing group ends
? match previous non-capturing group 0 or 1 time
[^\\*]+ 1 or more times characters except *
\\*/ match */ one time
) 1st non-capturing group ends
public static String cleanComment(String str) {
int open = str.indexOf("/*"), close = str.indexOf("*/");
if( (open&close) < 0 ) return str;
open &= Integer.MAX_VALUE;
close &= Integer.MAX_VALUE;
if(open < close) {
if(close > str.length()) {
return str.substring(0, open);
} else {
return str.substring(0, open) + cleanComment( str.substring(close+2) );
}
} else {
return cleanComment( str.substring(close+2) );
}
}