我试图从Oracle网站中提取JDK下载URL列表。我最初在Go中编写了一个工作版本并尝试将其移植到Groovy,因此它将在Jenkins中运行。正则表达式在Groovy中不匹配。所以我用Java编写了一个版本进行故障排除:
以下Java代码成功:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
StringBuffer contents = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null)
contents.append(inputLine+"\n");
in.close();
Pattern pattern = Pattern.compile("(http://download\\.oracle\\.com/otn/java/jdk/\\du\\d{2}-b\\d{1,2}/(jdk-\\du\\d{2}-(linux|windows)-x64\\.(exe|tar\\.gz)))");
Matcher matcher = pattern.matcher(contents);
assert matcher.matches();
}
}
然而(希望等效)Groovy失败了:
URL url = new URL('http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html')
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer contents = new StringBuffer();
String inputLine
while ((inputLine = bufferedReader.readLine()) != null)
contents.append(inputLine + "\n");
bufferedReader.close();
def pattern = ~/(http\/\/download\.oracle\.com\/otn\/java\/jdk\/\du\d{2}-b\d{1,2}\/(jdk-\du\d{2}-(linux\|windows)-x64\.(exe\|tar\.gz)))/
def matcher = pattern.matcher(contents)
assert matcher.matches()
我试图最小化Groovy和Java版本之间的差异以进行故障排除(是的 - 我知道在Groovy中有更多惯用的方法来诋毁网址)。
任何人都知道为什么Groovy版本会失败?
答案 0 :(得分:0)
您最大的问题是您在第二个正则表达式中缺少:
(您有http\/
而不是http:\/
)。在那之后,看起来它们应该匹配。