Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在字符串中查找不同日期的数目_Regex - Fatal编程技术网

Regex 在字符串中查找不同日期的数目

Regex 在字符串中查找不同日期的数目,regex,Regex,我陷入了一个问题。一个包含一些日期的段落,格式为DD-MM-YYYY,可以在该段落中找到不同的年份数。这可以通过使用regex来解决吗?regex for datevar regex=/(?:(?:[0-2][0-9]|[3][01])-(?:[0][13578][1][02])|(?:[0-2][0-9]| 30-(?:[0][469]|(?:[0-2][0-2][0-9]-[0-9]{4] 上述正则表达式不能与闰年匹配 在JS中查找pargraph中的日期计数是 function dateC

我陷入了一个问题。一个包含一些日期的段落,格式为DD-MM-YYYY,可以在该段落中找到不同的年份数。这可以通过使用regex来解决吗?

regex for date
var regex=/(?:(?:[0-2][0-9]|[3][01])-(?:[0][13578][1][02])|(?:[0-2][0-9]| 30-(?:[0][469]|(?:[0-2][0-2][0-9]-[0-9]{4]

上述正则表达式不能与闰年匹配

在JS中查找pargraph中的日期计数是

function dateCount(input){
    const regex = /(?:(?:[0-2][0-9]|[3][01])-(?:[0][13578]|[1][02])|(?:[0-2][0-9]|30)-(?:[0][469]|11)|(?:[0-2][0-9])-02)-[0-9]{4}/g;
    let m;
    var noDates= 0; 
    while ((match = regex.exec(input)) !== null) {
        dateCounter++; 
//This is to prevent from running into infinite loop
        if (match.index === regex.lastIndex) {
            regex.lastIndex++;
        }
    }
    console.log(dateCounter);
}

纯正则表达式本身不能做到这一点,但正则表达式可以用来分离和提取日期。您使用的是什么编程语言或工具?另外,请发布一些相同的输入和输出示例。我使用的是JAVA。一些日期为1990年1月12日的随机词,还有一些日期为2017年12月12日的随机词,还有一些日期为1990年5月12日的随机词。预期输出:3,因为它包含3个日期读取此: