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 正则表达式,我怎样才能设法获得具有如下模式的字符串的子字符串:
data:image/png;base64,iVBO....{a very long string of characters and symbols}
但是,我需要到达image/png的地方可能是image/jpg或image/xxxx
image/png
image/jpg
image/xxxx
您可以尝试使用简单的正则表达式:
图片/\w+
String str = "data:image/png;base64,iVBO...."; Matcher m = Pattern.compile("image/\\w+").matcher(str); while (m.find()) System.out.println(m.group());
图片/png
如果你想要的内容\w+(我想可能是这种情况),你可以将它放在一个捕获组中:
\w+
图像/(\w+)
并打电话m.group(1)。
m.group(1)