解析单行块注释
Parse block comment on single line
我想ignore/target一行的非块注释段。
例如,以下字符串需要 ALL 结果为字符串 "foobar"
"foo/*comment*/bar"
"comm*/foobar/*ent"
"comment*/foobar"
"foobar/*comment"
实现这个的最佳方法是什么?
编辑请试试这个:
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);
}
}
输出:
foobar
foobar
foobar
foobar
正则表达式的解释如下:
(?: 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 具有相同的逻辑,但以递归形式实现以满足您对简单性的渴望:
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) );
}
}
我想ignore/target一行的非块注释段。
例如,以下字符串需要 ALL 结果为字符串 "foobar"
"foo/*comment*/bar"
"comm*/foobar/*ent"
"comment*/foobar"
"foobar/*comment"
实现这个的最佳方法是什么?
编辑请试试这个:
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);
}
}
输出:
foobar
foobar
foobar
foobar
正则表达式的解释如下:
(?: 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) );
}
}