使用字符串 [java] 解析文件名
parse filename using string [java]
我需要什么正则表达式模式来解析这样的文件名:"Ab12_Cd9023-2000-12-04-No234.nekiRtt3434GGG"
,其中解析的元素是:"Ab12_Cd9023"
(name), "2000"
(year), "12"
(月),"04"
(日),"234"
(数字),"nekiRtt3434GGG"
(后缀)。顺序始终相同:name-yyyy-MM-dd-NoNN.suffix
.
我想使用 pattern
+ matcher
对象来解决这个问题。
这个正则表达式应该可以解决问题:
([a-bA-B0-9_])-([0-9]{4})-([0-9]{2})-([04]{2})-No(.+)\.(.+)$
如果您将其用作模式,则每个 () 表示您要捕获的字符串的一部分。
这可行:
private static final Pattern PATTERN = Pattern.compile("^(.+)-([0-9]{4})-([0-9]{2})-([0-9]{2})-No(.+)\.(.+)$");
...
Matcher matcher = PATTERN.matcher(string);
if (matcher.matches()) {
String name = matcher.group(1);
int year = Integer.parseInt(matcher.group(2));
int month = Integer.parseInt(matcher.group(3));
int day = Integer.parseInt(matcher.group(4));
String number = matcher.group(5);
String suffix = matcher.group(6);
System.out.println("name: " + name);
System.out.println("year: " + year);
System.out.println("month: " + month);
System.out.println("day: " + day);
System.out.println("number: " + number);
System.out.println("suffix: " + suffix);
} else {
// error: does not match
}
如果序列总是相同的,为什么不使用 - 或 .像这样:
String filename = "Ab12_Cd9023-2000-12-04-No234.nekiRtt3434GGG";
String[] parts = filename.split("-|\.");
for(String p : parts)
System.out.println(p);
这是我找到的最漂亮的解决方案:
private static final Pattern PATTERN = Pattern.compile("^(?<name>\w+)-"
+ "(?<year>\d{4})-"
+ "(?<month>\d{2})-"
+ "(?<day>\d{2})-"
+ "No(?<number>\d+)."
+ "(?<suffix>\w+)$");
Matcher m = PATTERN.matcher(file.getName());
if(!m.matches())
//some code if the pattern doesnt match
//this is how you acces the parsed strings:
m.group("year")
我需要什么正则表达式模式来解析这样的文件名:"Ab12_Cd9023-2000-12-04-No234.nekiRtt3434GGG"
,其中解析的元素是:"Ab12_Cd9023"
(name), "2000"
(year), "12"
(月),"04"
(日),"234"
(数字),"nekiRtt3434GGG"
(后缀)。顺序始终相同:name-yyyy-MM-dd-NoNN.suffix
.
我想使用 pattern
+ matcher
对象来解决这个问题。
这个正则表达式应该可以解决问题:
([a-bA-B0-9_])-([0-9]{4})-([0-9]{2})-([04]{2})-No(.+)\.(.+)$
如果您将其用作模式,则每个 () 表示您要捕获的字符串的一部分。
这可行:
private static final Pattern PATTERN = Pattern.compile("^(.+)-([0-9]{4})-([0-9]{2})-([0-9]{2})-No(.+)\.(.+)$");
...
Matcher matcher = PATTERN.matcher(string);
if (matcher.matches()) {
String name = matcher.group(1);
int year = Integer.parseInt(matcher.group(2));
int month = Integer.parseInt(matcher.group(3));
int day = Integer.parseInt(matcher.group(4));
String number = matcher.group(5);
String suffix = matcher.group(6);
System.out.println("name: " + name);
System.out.println("year: " + year);
System.out.println("month: " + month);
System.out.println("day: " + day);
System.out.println("number: " + number);
System.out.println("suffix: " + suffix);
} else {
// error: does not match
}
如果序列总是相同的,为什么不使用 - 或 .像这样:
String filename = "Ab12_Cd9023-2000-12-04-No234.nekiRtt3434GGG";
String[] parts = filename.split("-|\.");
for(String p : parts)
System.out.println(p);
这是我找到的最漂亮的解决方案:
private static final Pattern PATTERN = Pattern.compile("^(?<name>\w+)-"
+ "(?<year>\d{4})-"
+ "(?<month>\d{2})-"
+ "(?<day>\d{2})-"
+ "No(?<number>\d+)."
+ "(?<suffix>\w+)$");
Matcher m = PATTERN.matcher(file.getName());
if(!m.matches())
//some code if the pattern doesnt match
//this is how you acces the parsed strings:
m.group("year")