Regular expression to extract file name

I have a web based text response and need to extract the file name. Any suggestions for a good RegEx?

Total parts : 1 Name : file Content Type : text/plain Size : 1167 content-type : text/plain content-disposition : form-data; name="file"; filename="test_example.txt" 
+4
source share
1 answer

You can use this regular expression to get the file name

 (?<=filename=").*?(?=") 

The code will look like this

 String fileName = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(requestHeaderString); if (regexMatcher.find()) { fileName = regexMatcher.group(); } 

Regular expression explanation

 (?<= # 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 ) 
+14
source

Source: https://habr.com/ru/post/1380736/


All Articles