使用正则表达式从日期格式字符串中删除元素
Remove elements from Date Format String using a Regular Expression
我想删除提供的日期格式字符串的元素 - 例如,通过删除任何 non-M/y 元素将格式 "dd/MM/yyyy" 转换为 "MM/yyyy"。
我想做的是根据为区域设置提供的现有日期/month/year 格式创建本地化的 month/year 格式。
我已使用正则表达式完成此操作,但解决方案似乎比我预期的要长。
示例如下:
public static void main(final String[] args) {
System.out.println(filterDateFormat("dd/MM/yyyy HH:mm:ss", 'M', 'y'));
System.out.println(filterDateFormat("MM/yyyy/dd", 'M', 'y'));
System.out.println(filterDateFormat("yyyy-MMM-dd", 'M', 'y'));
}
/**
* Removes {@code charsToRetain} from {@code format}, including any redundant
* separators.
*/
private static String filterDateFormat(final String format, final char...charsToRetain) {
// Match e.g. "ddd-"
final Pattern pattern = Pattern.compile("[" + new String(charsToRetain) + "]+\p{Punct}?");
final Matcher matcher = pattern.matcher(format);
final StringBuilder builder = new StringBuilder();
while (matcher.find()) {
// Append each match
builder.append(matcher.group());
}
// If the last match is "mmm-", remove the trailing punctuation symbol
return builder.toString().replaceFirst("\p{Punct}$", "");
}
我将尝试根据对我的问题的理解来回答:如何从字符串的 list/table/array 中删除不完全遵循模式 'dd/MM'.[=13= 的元素]
所以我正在寻找一个类似于
的函数
public List<String> removeUnWantedDateFormat(List<String> input)
根据我对 Dateformat 的了解,我们可以预期只有 4 种可能性是您想要的,希望我不会错过任何一种,它们是 "MM/yyyy"、"MMM/yyyy"、"MM/yy"、"MM/yyyy"。为了让我们知道我们在寻找什么,我们可以做一个简单的功能。
public List<String> removeUnWantedDateFormat(List<String> input) {
String s1 = "MM/yyyy";
string s2 = "MMM/yyyy";
String s3 = "MM/yy";
string s4 = "MMM/yy";
for (String format:input) {
if (!s1.equals(format) && s2.equals(format) && s3.equals(format) && s4.equals(format))
input.remove(format);
}
return input;
}
如果可以,最好不要使用正则表达式,它会消耗大量资源。很大的改进是使用您接受的日期格式的枚举,这样您可以更好地控制它,甚至替换它们。
希望这会有所帮助,干杯
编辑:在我看到评论后,我认为使用 contains 而不是 equals 会更好,应该像一个魅力而不是删除,
输入 = 预期的字符串。
所以它看起来更像:
public List<String> removeUnWantedDateFormat(List<String> input) {
List<String> comparaisons = new ArrayList<>();
comparaison.add("MMM/yyyy");
comparaison.add("MMM/yy");
comparaison.add("MM/yyyy");
comparaison.add("MM/yy");
for (String format:input) {
for(String comparaison: comparaisons)
if (format.contains(comparaison)) {
format = comparaison;
break;
}
}
return input;
}
让我们尝试以下日期格式字符串的解决方案:
String[] formatStrings = { "dd/MM/yyyy HH:mm:ss",
"MM/yyyy/dd",
"yyyy-MMM-dd",
"MM/yy - yy/dd",
"yyabbadabbadooMM" };
下面将分析匹配的字符串,然后打印匹配的第一组。
Pattern p = Pattern.compile(REGEX);
for(String formatStr : formatStrings) {
Matcher m = p.matcher(formatStr);
if(m.matches()) {
System.out.println(m.group(1));
}
else {
System.out.println("Didn't match!");
}
}
现在,我尝试了两个独立的正则表达式。第一:
final String REGEX = "(?:[^My]*)([My]+[^\w]*[My]+)(?:[^My]*)";
有程序输出:
MM/yyyy
MM/yyyy
yyyy-MMM
Didn't match!
Didn't match!
第二个:
final String REGEX = "(?:[^My]*)((?:[My]+[^\w]*)+[My]+)(?:[^My]*)";
有程序输出:
MM/yyyy
MM/yyyy
yyyy-MMM
MM/yy - yy
Didn't match!
现在,让我们看看第一个正则表达式实际匹配的是什么:
(?:[^My]*)([My]+[^\w]*[My]+)(?:[^My]*) First regex =
(?:[^My]*) Any amount of non-Ms and non-ys (non-capturing)
([My]+ followed by one or more Ms and ys
[^\w]* optionally separated by non-word characters
(implying they are also not Ms or ys)
[My]+) followed by one or more Ms and ys
(?:[^My]*) finished by any number of non-Ms and non-ys
(non-capturing)
这意味着至少需要 2 M/ys 来匹配正则表达式,尽管您应该注意像 MM-dd 或 yy-DD 这样的东西也会匹配,因为它们有两个 M -or-y 区域 1 个字符长。您可以通过对日期格式字符串进行健全性检查来避免在这里遇到麻烦,例如:
if(formatStr.contains('y') && formatStr.contains('M') && m.matches())
{
String yMString = m.group(1);
... // other logic
}
至于第二个正则表达式,这是它的意思:
(?:[^My]*)((?:[My]+[^\w]*)+[My]+)(?:[^My]*) Second regex =
(?:[^My]*) Any amount of non-Ms and non-ys
(non-capturing)
( ) followed by
(?:[My]+ )+[My]+ at least two text segments consisting of
one or more Ms or ys, where each segment is
[^\w]* optionally separated by non-word characters
(?:[^My]*) finished by any number of non-Ms and non-ys
(non-capturing)
此正则表达式将匹配范围稍广的一系列字符串,但它仍然要求 Ms 和 ys 之间的任何分隔都是非单词 ([^a-zA-Z_0-9]
)。此外,请记住,此正则表达式仍将匹配 "yy"、"MM" 或类似的字符串,如 "yyy"、"yyyy"...,因此使用如前一个正则表达式所述的健全性检查。
此外,这里有一个简单的例子,说明如何使用上面的方法来操作单个日期格式字符串:
LocalDateTime date = LocalDateTime.now();
String dateFormatString = "dd/MM/yyyy H:m:s";
System.out.println("Old Format: \"" + dateFormatString + "\" = " +
date.format(DateTimeFormatter.ofPattern(dateFormatString)));
Pattern p = Pattern.compile("(?:[^My]*)([My]+[^\w]*[My]+)(?:[^My]*)");
Matcher m = p.matcher(dateFormatString);
if(dateFormatString.contains("y") && dateFormatString.contains("M") && m.matches())
{
dateFormatString = m.group(1);
System.out.println("New Format: \"" + dateFormatString + "\" = " +
date.format(DateTimeFormatter.ofPattern(dateFormatString)));
}
else
{
throw new IllegalArgumentException("Couldn't shorten date format string!");
}
输出:
Old Format: "dd/MM/yyyy H:m:s" = 14/08/2019 16:55:45
New Format: "MM/yyyy" = 08/2019
我想删除提供的日期格式字符串的元素 - 例如,通过删除任何 non-M/y 元素将格式 "dd/MM/yyyy" 转换为 "MM/yyyy"。
我想做的是根据为区域设置提供的现有日期/month/year 格式创建本地化的 month/year 格式。
我已使用正则表达式完成此操作,但解决方案似乎比我预期的要长。
示例如下:
public static void main(final String[] args) {
System.out.println(filterDateFormat("dd/MM/yyyy HH:mm:ss", 'M', 'y'));
System.out.println(filterDateFormat("MM/yyyy/dd", 'M', 'y'));
System.out.println(filterDateFormat("yyyy-MMM-dd", 'M', 'y'));
}
/**
* Removes {@code charsToRetain} from {@code format}, including any redundant
* separators.
*/
private static String filterDateFormat(final String format, final char...charsToRetain) {
// Match e.g. "ddd-"
final Pattern pattern = Pattern.compile("[" + new String(charsToRetain) + "]+\p{Punct}?");
final Matcher matcher = pattern.matcher(format);
final StringBuilder builder = new StringBuilder();
while (matcher.find()) {
// Append each match
builder.append(matcher.group());
}
// If the last match is "mmm-", remove the trailing punctuation symbol
return builder.toString().replaceFirst("\p{Punct}$", "");
}
我将尝试根据对我的问题的理解来回答:如何从字符串的 list/table/array 中删除不完全遵循模式 'dd/MM'.[=13= 的元素]
所以我正在寻找一个类似于
的函数public List<String> removeUnWantedDateFormat(List<String> input)
根据我对 Dateformat 的了解,我们可以预期只有 4 种可能性是您想要的,希望我不会错过任何一种,它们是 "MM/yyyy"、"MMM/yyyy"、"MM/yy"、"MM/yyyy"。为了让我们知道我们在寻找什么,我们可以做一个简单的功能。
public List<String> removeUnWantedDateFormat(List<String> input) {
String s1 = "MM/yyyy";
string s2 = "MMM/yyyy";
String s3 = "MM/yy";
string s4 = "MMM/yy";
for (String format:input) {
if (!s1.equals(format) && s2.equals(format) && s3.equals(format) && s4.equals(format))
input.remove(format);
}
return input;
}
如果可以,最好不要使用正则表达式,它会消耗大量资源。很大的改进是使用您接受的日期格式的枚举,这样您可以更好地控制它,甚至替换它们。
希望这会有所帮助,干杯
编辑:在我看到评论后,我认为使用 contains 而不是 equals 会更好,应该像一个魅力而不是删除,
输入 = 预期的字符串。
所以它看起来更像:
public List<String> removeUnWantedDateFormat(List<String> input) {
List<String> comparaisons = new ArrayList<>();
comparaison.add("MMM/yyyy");
comparaison.add("MMM/yy");
comparaison.add("MM/yyyy");
comparaison.add("MM/yy");
for (String format:input) {
for(String comparaison: comparaisons)
if (format.contains(comparaison)) {
format = comparaison;
break;
}
}
return input;
}
让我们尝试以下日期格式字符串的解决方案:
String[] formatStrings = { "dd/MM/yyyy HH:mm:ss",
"MM/yyyy/dd",
"yyyy-MMM-dd",
"MM/yy - yy/dd",
"yyabbadabbadooMM" };
下面将分析匹配的字符串,然后打印匹配的第一组。
Pattern p = Pattern.compile(REGEX);
for(String formatStr : formatStrings) {
Matcher m = p.matcher(formatStr);
if(m.matches()) {
System.out.println(m.group(1));
}
else {
System.out.println("Didn't match!");
}
}
现在,我尝试了两个独立的正则表达式。第一:
final String REGEX = "(?:[^My]*)([My]+[^\w]*[My]+)(?:[^My]*)";
有程序输出:
MM/yyyy
MM/yyyy
yyyy-MMM
Didn't match!
Didn't match!
第二个:
final String REGEX = "(?:[^My]*)((?:[My]+[^\w]*)+[My]+)(?:[^My]*)";
有程序输出:
MM/yyyy
MM/yyyy
yyyy-MMM
MM/yy - yy
Didn't match!
现在,让我们看看第一个正则表达式实际匹配的是什么:
(?:[^My]*)([My]+[^\w]*[My]+)(?:[^My]*) First regex =
(?:[^My]*) Any amount of non-Ms and non-ys (non-capturing)
([My]+ followed by one or more Ms and ys
[^\w]* optionally separated by non-word characters
(implying they are also not Ms or ys)
[My]+) followed by one or more Ms and ys
(?:[^My]*) finished by any number of non-Ms and non-ys
(non-capturing)
这意味着至少需要 2 M/ys 来匹配正则表达式,尽管您应该注意像 MM-dd 或 yy-DD 这样的东西也会匹配,因为它们有两个 M -or-y 区域 1 个字符长。您可以通过对日期格式字符串进行健全性检查来避免在这里遇到麻烦,例如:
if(formatStr.contains('y') && formatStr.contains('M') && m.matches())
{
String yMString = m.group(1);
... // other logic
}
至于第二个正则表达式,这是它的意思:
(?:[^My]*)((?:[My]+[^\w]*)+[My]+)(?:[^My]*) Second regex =
(?:[^My]*) Any amount of non-Ms and non-ys
(non-capturing)
( ) followed by
(?:[My]+ )+[My]+ at least two text segments consisting of
one or more Ms or ys, where each segment is
[^\w]* optionally separated by non-word characters
(?:[^My]*) finished by any number of non-Ms and non-ys
(non-capturing)
此正则表达式将匹配范围稍广的一系列字符串,但它仍然要求 Ms 和 ys 之间的任何分隔都是非单词 ([^a-zA-Z_0-9]
)。此外,请记住,此正则表达式仍将匹配 "yy"、"MM" 或类似的字符串,如 "yyy"、"yyyy"...,因此使用如前一个正则表达式所述的健全性检查。
此外,这里有一个简单的例子,说明如何使用上面的方法来操作单个日期格式字符串:
LocalDateTime date = LocalDateTime.now();
String dateFormatString = "dd/MM/yyyy H:m:s";
System.out.println("Old Format: \"" + dateFormatString + "\" = " +
date.format(DateTimeFormatter.ofPattern(dateFormatString)));
Pattern p = Pattern.compile("(?:[^My]*)([My]+[^\w]*[My]+)(?:[^My]*)");
Matcher m = p.matcher(dateFormatString);
if(dateFormatString.contains("y") && dateFormatString.contains("M") && m.matches())
{
dateFormatString = m.group(1);
System.out.println("New Format: \"" + dateFormatString + "\" = " +
date.format(DateTimeFormatter.ofPattern(dateFormatString)));
}
else
{
throw new IllegalArgumentException("Couldn't shorten date format string!");
}
输出:
Old Format: "dd/MM/yyyy H:m:s" = 14/08/2019 16:55:45
New Format: "MM/yyyy" = 08/2019