我从正则表达式开始,但是对于我发现的东西,我认为最好以另一种方式进行。
我想从以下文本中获取图像名称的数组:
以下图像失败:
[T430040.tif, T432040.tif, T411030.tif, CH1090.tif, T432050.tif, T432090.tif, T432020.tif, CRP040.tif, T432070.tif, T040060.tif]
Array:
T430040
T432040
T411030
CH1090
T432050
T432090
T432020
CRP040
T432070
T040060
任何帮助将不胜感激。
答案 0 :(得分:3)
没有正则表达式:
.AddRedirect("^your-site.azurewebsites.net$", "https://www.Your-Site.com/{R:0}", StatusCodes.Status301MovedPermanently)
将打印数组:
String str = "The following images failed: [T430040.tif, T432040.tif, T411030.tif, CH1090.tif, T432050.tif, T432090.tif, T432020.tif, CRP040.tif, T432070.tif, T040060.tif]";
String[] array = str.substring(str.indexOf("[") + 1, str.length() - 1)
.replace(".tif", "")
.split(",");
System.out.println(Arrays.toString(array));
答案 1 :(得分:1)
此Java代码使用正则表达式\w+(?=\.)
来满足您的需求。
String s = "The following images failed: [T430040.tif, T432040.tif, T411030.tif, CH1090.tif, T432050.tif, T432090.tif, T432020.tif, CRP040.tif, T432070.tif, T040060.tif]";
Pattern p = Pattern.compile("\\w+(?=\\.)");
Matcher m = p.matcher(s);
while(m.find()) {
System.out.println(m.group());
}
然后打印
T430040
T432040
T411030
CH1090
T432050
T432090
T432020
CRP040
T432070
T040060