You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java检测文本行是否包含指定双括号日期字符串模式?

Fixing Date Pattern Detection in Java

Let’s work through fixing your regex and completing the date detection functionality step by step.

Issues with Your Current Regex

Your existing regex "(?s)(\\[)(\\[)[0-9]+(\\s)[abc]+(\\])(\\])(?s)" has a few critical problems:

  • [abc]+ only matches lowercase a/b/c characters, which won’t work for month names like "Februari" (it includes uppercase letters and other characters).
  • It only targets a single [[...]] segment, but you need to detect paired segments like [[19 Februari]] [[1942]].
  • The (?s) flags are unnecessary here—they enable dotall mode, but you aren’t using the dot (.) metacharacter in your pattern.

Improved Regex

To match date pairs in the format [[DD Month]] [[YYYY]], use this refined regex:

String datePairRegex = "\\[\\[(\\d+ \\p{L}+)\\]\\] \\[\\[(\\d{4})\\]\\]";

Pattern Breakdown:

  • \\[\\[: Matches the opening double brackets (escaped for Java string syntax).
  • (\\d+ \\p{L}+): Captures the day and month (one or more digits, a space, then one or more Unicode letters to support non-English months like "Februari").
  • \\]\\]: Matches the closing double brackets.
  • : Matches the space between the two date components.
  • \\[\\[(\\d{4})\\]\\]: Captures the 4-digit year inside double brackets.

If you need a more flexible pattern to match any two double-bracket segments separated by a space (not just day-month + year), use:

String flexibleRegex = "\\[\\[([^\\]]+)\\]\\] \\[\\[([^\\]]+)\\]\\]";

Complete Java Code

Here’s the finished method that detects and extracts all valid date pairs from your input string:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateDetector {
    public static void dumpDateTagger(String inputString) {
        // Check for date range separators
        if (inputString.contains("–") || inputString.contains("-")) {
            // Compile the regex pattern
            Pattern pattern = Pattern.compile("\\[\\[(\\d+ \\p{L}+)\\]\\] \\[\\[(\\d{4})\\]\\]");
            Matcher matcher = pattern.matcher(inputString);
            
            // Iterate through all matches
            while (matcher.find()) {
                String fullDatePair = matcher.group(0); // Entire matched pair (e.g., [[19 Februari]] [[1942]])
                String dayMonth = matcher.group(1); // e.g., 19 Februari
                String year = matcher.group(2); // e.g., 1942
                
                System.out.println("Detected date pair: " + fullDatePair);
                System.out.println("Day & Month: " + dayMonth);
                System.out.println("Year: " + year);
                System.out.println("---");
            }
        } else {
            System.out.println("No date range separator found in the input string.");
        }
    }

    public static void main(String[] args) {
        String testInput = "|date=[[19 Februari]] [[1942]] – [[10 Februari]] [[1943]]";
        dumpDateTagger(testInput);
    }
}

Sample Output:

Detected date pair: [[19 Februari]] [[1942]]
Day & Month: 19 Februari
Year: 1942
---
Detected date pair: [[10 Februari]] [[1943]]
Day & Month: 10 Februari
Year: 1943
---

Key Notes

  • Use \\p{L} instead of [a-zA-Z] to support Unicode letters, which is essential for non-English month names.
  • The while (matcher.find()) loop ensures all date pairs in the string are detected, not just the first occurrence.
  • If you need to handle optional spaces between segments, add \\s* (zero or more spaces) to the regex (e.g., \\[\\[(\\d+ \\p{L}+)\\]\\]\\s*\\[\\[(\\d{4})\\]\\]).

内容的提问来源于stack exchange,提问作者Acantya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:43:14