-1

I am stuck in a question. A paragraph which contained some dates in format DD-MM-YYYY, find number of distinct years in the paragraph .Can this be solved using regex?

1 Answers1

0

Regex for date var 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;.

The above regex cannot match leap years.

To find the count of dates in the pargraph in JS is

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);
}
AbhishekGowda28
  • 994
  • 8
  • 19