您可以使用此正則表達式來獲取文件名
(?<=filename=").*?(?=")
代碼看起來像這樣
String fileName = null;
Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")");
Matcher regexMatcher = regex.matcher(requestHeaderString);
if (regexMatcher.find()) {
fileName = regexMatcher.group();
}
正則表達式
(?<= # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
filename=" # Match the characters 「filename="」 literally
)
. # Match any single character that is not a line break character
*? # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
" # Match the character 「"」 literally
)
的說明請參見各行,如果它包含()''文件名'然後只是'分割()'它與文件名=' –