Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 java 中使用正则表达式来执行以下操作?
String code = "import java.io.*;" + "import java.util.*;" + "public class Test1 extends Exam{" + " // my code " + "}";
从上面的字符串中,我怎样才能准确地获得类名Test1。
使用这个正则表达式:
String className = code.replaceAll("(?s)^.*?(?:public|protected|private)?\\s*(?:\\s+static\\s+)?class\\s+(\\S+).*$", "$1"); //=> Test1