我一直在尝试编写一个正则表达式来匹配一个类的导入。让类成为
import static org.junit.Assert.*;
import org.
package.
Test;
import mypackage.mystuff;
输出应为[org.junit.Assert。*,org.package.Test,mypackage.mystuff]。我一直在换行符和正则表达式方面苦苦挣扎,因为我对换行符还没有足够的经验。这是我目前的尝试:
((?<=\bimport\s)\s*([^\s]+ )*([a-z.A-Z0-9]+.(?=;)))
最佳答案
此正则表达式适用于所有类型的导入语句,并且不应与无效语句匹配:import\p{javaIdentifierIgnorable}*\p{javaWhitespace}+(?:static\p{javaIdentifierIgnorable}*\p{javaWhitespace}+)?(\p{javaJavaIdentifierStart}[\p{javaJavaIdentifierPart}\p{javaIdentifierIgnorable}]*(?:\p{javaWhitespace}*\.\p{javaWhitespace}*\*|(?:\p{javaWhitespace}*\.\p{javaWhitespace}*\p{javaJavaIdentifierStart}[\p{javaJavaIdentifierPart}\p{javaIdentifierIgnorable}]*)+(?:\p{javaWhitespace}*\.\p{javaWhitespace}*\*)?))\p{javaWhitespace}*;
它广泛使用Java's categories,例如\p{javaWhitespace}
调用Character.isWhitespace:
还是不可读?如此猜测。这就是为什么我尝试用Java代码(REGEX
)来表达它的原因:
public class ImportMatching {
static final String IMPORTS = "import\n" +
"java.io.IOException;\n" +
"import java.nio.file.Files;\n" +
"import java . nio . file. Path;\n" +
"import java.nio.file.Paths\n" +
";import java.util.ArrayList;\n" +
"import static java.util. List.*;\n" +
"import java.util.List. *;\n" +
"import java.\n" +
" util.\n" +
" List;\n" +
" import java.util.regex.Matcher;import java.util.regex.Pattern\n" +
" ;\n" +
"import mypackage.mystuff;\n" +
"import mypackage.*;";
static final String WS = "\\p{javaWhitespace}";
static final String IG = "\\p{javaIdentifierIgnorable}";
static final String ID = "\\p{javaJavaIdentifierStart}" + multiple(charClass("\\p{javaJavaIdentifierPart}" + IG));
static final String DOT = multiple(WS) + "\\." + multiple(WS);
static final String WC = "\\*";
static final String REGEX = "import" + multiple(IG) + atLeastOnce(WS) +
optional(nonCapturingGroup("static" + multiple(IG) + atLeastOnce(WS))) +
group(
ID +
nonCapturingGroup(
or(
DOT + WC,
atLeastOnce(nonCapturingGroup(DOT + ID)) + optional(nonCapturingGroup(DOT + WC))
)
)
) +
multiple(WS) + ';';
public static void main(String[] args) {
final List<String> imports = getImports(IMPORTS);
System.out.printf("Matches: %d%n", imports.size());
imports.stream().forEach(System.out::println);
}
static List<String> getImports(String javaSource) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(javaSource);
List<String> imports = new ArrayList<>();
while(matcher.find()) {
imports.add(matcher.group(1).replaceAll(charClass(WS + IG), ""));
}
return imports;
}
static String nonCapturingGroup(String regex) {
return group("?:" + regex);
}
static String or(String option1, String option2) {
return option1 + '|' + option2;
}
static String atLeastOnce(String regex) {
return regex + '+';
}
static String optional(String regex) {
return regex + '?';
}
static String multiple(String regex) {
return regex + '*';
}
static String group(String regex) {
return '(' + regex + ')';
}
static String charClass(String regex) {
return '[' + regex + ']';
}
}
我将一组用于
package.Class
部分,然后替换匹配项中的所有噪音。测试输入是以下字符串(
IMPORTS
):import
java.io.IOException;
import java.nio.file.Files;
import java . nio . file. Path;
import java.nio.file.Paths
;import java.util.ArrayList;
import static java.util. List.*;
import java.util.List. *;
import java.
util.
List;
import java.util.regex.Matcher;import java.util.regex.Pattern
;
import mypackage.mystuff;
import mypackage.*;
输出:
符合条件:12
java.io.IOException
java.nio.file.Files
java.nio.file.Path
java.nio.file.Paths
java.util.ArrayList
java.util.List。*
java.util.List。*
java.util.List
java.util.regex.Matcher
java.util.regex.Pattern
mypackage.mystuff
mypackage。*
关于java - 正则表达式匹配某类的导入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36154500/