如何修复日期正则表达式?

huangapple go评论108阅读模式
英文:

how to fix the date regex?

问题

  1. 我试图仅使用正则表达式匹配`dd/MM/yyyy`日期格式但如果我输入`dd/MM/yy`它也会被接受我想知道如何使其只接受`dd/MM/yyyy`格式的值
  2. 如果我使用
  3. ```Java
  4. new SimpleDateFormat("dd/MM/yyyy").parse(value);

代码仍然会接受dd/MM/yy格式。

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Collection;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. public class Main {
  7. public static void main(String[] args) {
  8. String value = "10/02/20";
  9. Pattern pattern = Pattern.compile("^([0-2][0-9]||3[0-1])/(0[0-9]||1[0-2])/([0-9][0-9])?[0-9][0-9]$");
  10. Matcher matcher = pattern.matcher(value);
  11. System.out.println(matcher.find());
  12. }
  13. }
  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to use the regex only for the `dd/MM/yyyy` date format but if I enter the `dd/MM/yy` value it accepts. I would like to know how to make it accept only the `dd/MM/yyyy` values.
  4. If I use
  5. ```Java
  6. new SimpleDateFormat(&quot;dd/MM/yyyy&quot;).parse(value);

the code continues to accept dd/MM/yy format.

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Collection;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. public class Main {
  7. public static void main(String[] args) {
  8. String value = &quot;10/02/20&quot;;
  9. Pattern pattern = Pattern.compile(&quot;^([0-2][0-9]||3[0-1])/(0[0-9]||1[0-2])/([0-9][0-9])?[0-9][0-9]$&quot;);
  10. Matcher matcher = pattern.matcher(value);
  11. System.out.println(matcher.find());
  12. }
  13. }

答案1

得分: 1

我建议您从过时且容易出错的 java.util 日期时间 API 和 SimpleDateFormat 切换到现代的 java.time 日期时间 API 和相应的格式化 API(包名为 java.time.format)。从 Trail: Date Time 了解更多关于现代日期时间 API 的信息。

  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeParseException;
  4. import java.util.Scanner;
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. // 定义格式
  9. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
  10. String dateString;
  11. LocalDate date = null;
  12. boolean valid;
  13. do {
  14. valid = true;
  15. System.out.print("输入格式为 dd/MM/yyyy 的日期:");
  16. dateString = scanner.nextLine();
  17. try {
  18. // 尝试解析输入字符串
  19. date = LocalDate.parse(dateString, formatter);
  20. } catch (DateTimeParseException e) {
  21. System.out.println("无效格式");
  22. // 异常时将 `valid` 设置为 false
  23. valid = false;
  24. }
  25. } while (!valid); // 只要 valid 为 false 就循环
  26. System.out.println("以 ISO-8601 格式的日期:" + date);
  27. System.out.println("以 dd/MM/yyyy 格式的日期:" + date.format(formatter));
  28. }
  29. }

示例运行:

  1. 输入格式为 dd/MM/yyyy 的日期:09/09/20
  2. 无效格式
  3. 输入格式为 dd/MM/yyyy 的日期:09/09/2020
  4. ISO-8601 格式的日期:2020-09-09
  5. dd/MM/yyyy 格式的日期:09/09/2020

正则表达式解决方案:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. String dateString;
  6. boolean valid;
  7. do {
  8. valid = true;
  9. System.out.print("输入格式为 dd/MM/yyyy 的日期:");
  10. dateString = scanner.nextLine();
  11. if (!dateString.matches("\\d{2}/\\d{2}/\\d{4}")) {
  12. System.out.println("无效格式");
  13. valid = false;
  14. }
  15. } while (!valid); // 只要 valid 为 false 就循环
  16. System.out.println(dateString);
  17. }
  18. }

示例运行:

  1. 输入格式为 dd/MM/yyyy 的日期:09/09/20
  2. 无效格式
  3. 输入格式为 dd/MM/yyyy 的日期:09/09/2020
  4. 09/09/2020

正则表达式解释:

  1. \d 表示数字
  2. X{n} 表示 X 恰好出现 n
英文:

I recommend you switch from the outdated and error-prone java.util date-time API and SimpleDateFormat to the modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.

  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeParseException;
  4. import java.util.Scanner;
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. // Define the format
  9. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;dd/MM/yyyy&quot;);
  10. String dateString;
  11. LocalDate date = null;
  12. boolean valid;
  13. do {
  14. valid = true;
  15. System.out.print(&quot;Enter a date in dd/MM/yyyy format: &quot;);
  16. dateString = scanner.nextLine();
  17. try {
  18. // Try to parse the input string
  19. date = LocalDate.parse(dateString, formatter);
  20. } catch (DateTimeParseException e) {
  21. System.out.println(&quot;Invalid format&quot;);
  22. // Set `valid` to false in case of exception
  23. valid = false;
  24. }
  25. } while (!valid);// Loop as long as valid is false
  26. System.out.println(&quot;Date in ISO-8601 format: &quot; + date);
  27. System.out.println(&quot;Date in dd/MM/yyyy format: &quot; + date.format(formatter));
  28. }
  29. }

A sample run:

  1. Enter a date in dd/MM/yyyy format: 09/09/20
  2. Invalid format
  3. Enter a date in dd/MM/yyyy format: 09/09/2020
  4. Date in ISO-8601 format: 2020-09-09
  5. Date in dd/MM/yyyy format: 09/09/2020

Regex Solution:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. String dateString;
  6. boolean valid;
  7. do {
  8. valid = true;
  9. System.out.print(&quot;Enter a date in dd/MM/yyyy format: &quot;);
  10. dateString = scanner.nextLine();
  11. if (!dateString.matches(&quot;\\d{2}\\/\\d{2}\\/\\d{4}&quot;)) {
  12. System.out.println(&quot;Invalid format&quot;);
  13. valid = false;
  14. }
  15. } while (!valid);// Loop as long as valid is false
  16. System.out.println(dateString);
  17. }
  18. }

A sample run:

  1. Enter a date in dd/MM/yyyy format: 09/09/20
  2. Invalid format
  3. Enter a date in dd/MM/yyyy format: 09/09/2020
  4. 09/09/2020

Explanation of the regex:

  1. \d specifies a digit
  2. X{n} speciifies X, exactly n times

huangapple
  • 本文由 发表于 2020年9月9日 19:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63811019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定