如果我想在字符串之间取日期,请任何人帮助我吗?
Print Date: 2011/12/23 (YYYY/MM/DD)
我只是想把日期放在这里,我需要将格式转换为DD / MM / YYYY。
答案 0 :(得分:1)
如果您使用C#
,此代码将执行Regex readdate = new Regex(@"(?<Year>(?:\d{4}|\d{2}))/(?<Month>\d{1,2})/(?<Day>\d{1,2})");
Console.WriteLine(DateTime.Parse(readdate.Match("Print Date: 2011/12/23").ToString()));
Console.ReadLine();
答案 1 :(得分:1)
在perl:
$str = 'Print Date: 2011/12/23 (YYYY/MM/DD)';
$date = $str =~ s#^\D+(\d{4})/\d{}/\d{2}.*$#$3/$2/$1#;
答案 2 :(得分:0)
试试这个:
public static String convertDateString(String s) {
Pattern p = Pattern.compile("(\\d{4})/(\\d{2})/(\\d{2})");
Matcher m = p.matcher(s);
if (m.find()) {
return m.group(3) + "/" + m.group(2) + "/" + m.group(1);
}
return null;
}
// ...
convertDateString("2011/12/23"); // => "23/12/2011"
或者,如果你只想“就地”这样做,试试这个:
public static String convertDateString2(String s) {
StringBuilder buf = new StringBuilder();
Pattern p = Pattern.compile("(\\d{4})/(\\d{2})/(\\d{2})");
Matcher m = p.matcher(s);
if (m.find()) {
buf.append(s.substring(0, m.start(1)));
buf.append(m.group(3)).append("/");
buf.append(m.group(2)).append("/");
buf.append(m.group(1)).append(s.substring(m.end(3)));
return buf.toString();
}
return null;
}
// ...
convertDateString2("Print Date: 2011/12/23.");
// => "Print Date: 23/12/2011."