当我提取 Java 类中方法的注释
时,我收到以下 String
响应:
@client.test.annotations.TestInfo(id=[C10137])
@org.testng.annotations.Test(alwaysRun=false, expectedExceptions=[]..
但是,我只对 id=[C10137]
部分感兴趣,并且想要获取该号码 - 10137
。还可以有这样的情况:
CASE1: //多个 id
@client.test.annotations.TestInfo(id=[C10137, C12121])
@org.testng.annotations.Test(alwaysRun=true,...
情况2: //没有ID
@client.test.annotations.TestInfo(id=[]) //ignore this time
@org.testng.annotations.Test(alwaysRun=true,...
正则表达式可以为我生成这个 id 数组吗?或者其他一些好的方法来获取所需的 id 数组。
最佳答案
您可以使用此正则表达式
\bid\b=\[(.+?)\]
<强> Regex Demo
Java 代码
String line = "@client.test.annotations.TestInfo(id=[C10137])@org.testng.annotations.Test(alwaysRun=false, expectedExceptions=[]..";
String pattern = "\\bid\\b=\\[(.+?)\\]";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println(m.group(1));
}
<强> Ideone Demo
关于java - 获取Java注解所需的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338435/