Sunday, November 1, 2015

Split Phone Numbers RegEx

Split Phone Numbers via Regex and Javascript on HackerRank:

 function processData(input) {  
   var lines = input.split("\n");  
     regex = /^(\d{1,3})[-?\s]?(\d{1,3})[-?\s]?(\d{4,10})/;  
   for (var i = 1; i < lines.length; i++) {  
     var str = lines[i].split(regex).filter(Boolean);  
     console.log("CountryCode=" + str[0] + ",LocalAreaCode=" + str[1] + ",Number=" + str[2]);  
   }  
 }   
 process.stdin.resume();  
 process.stdin.setEncoding("ascii");  
 _input = "";  
 process.stdin.on("data", function (input) {  
   _input += input;  
 });  
 process.stdin.on("end", function () {  
   processData(_input);  
 });  

Test results:
Test Case #0: 0.1s
Test Case #1: 0.09s
Test Case #2: 0.1s
Test Case #3: 0.09s
Test Case #4: 0.1s
Test Case #5: 0.1s
Test Case #6: 0.09s
Test Case #7: 0.09s
Test Case #8: 0.09s
Test Case #9: 0.09s
Test Case #10: 0.09s

No comments: