Home > Net >  Regex required for parsing text into object
Regex required for parsing text into object

Time:12-28

Hey guys i am sharing some snippet of text where i need to parse address key text.

PAYER:\r\n\r\n   United Con\r\n\r\nPROVIDER:\r\n\r\n   YAR KHAN \r\n   Provider ID: 11544974\r\n\r\nINSURED:\r\n\r\n   MICHELLE WICK\r\n   Member ID: 134557001\r\n   Address:\r\n      1103 CRESCENT OAK DR\r\n      MISRI CITY, TX 774\r\n   Birth Date: 05/22/1980\r\n

REGEX CODE

const obj = {},
  re = new RegExp("(.*?):\r\n(.*?)(?:\r\n)", "g");

y.replace(re, (_, key, value) => {
  obj[key.trim().replace(/\s /g, "").toLowerCase()] = value.trim();
});

console.log(obj);

CodePudding user response:

You can use

const regex = /(.*?):[\r\n]*(.*(?:\r?\n {4}.*)*)/g;

See the regex demo. Details:

  • (.*?) - Group 1: any zero or more chars other than line break chars as few as possible
  • : - a colon
  • [\r\n]* - zero or more linebreaks
  • (.*(?:\r?\n {4}.*)*) - Group 1:
    • .* - any zero or more chars other than line break chars as many as possible
    • (?:\r?\n {4}.*)* - zero or more sequences of a CRLF/LF line break and then four spaces and then any zero or more chars other than line break chars as many as possible.

See the JavaScript demo:

const text = 'PAYER:\r\n\r\n   United Con\r\n\r\nPROVIDER:\r\n\r\n   YAR KHAN \r\n   Provider ID: 11544974\r\n\r\nINSURED:\r\n\r\n   MICHELLE WICK\r\n   Member ID: 134557001\r\n   Address:\r\n      1103 CRESCENT OAK DR\r\n      MISRI CITY, TX 774\r\n   Birth Date: 05/22/1980\r\n';
const regex = /(.*?):[\r\n]*(.*(?:\r?\n {4}.*)*)/g;
let obj = {}, m;
while( m=regex.exec(text) ) {
  obj[m[1].trim().replace(/\s /g, " ").toLowerCase()] = m[2].trim();
}
console.log(obj)

  • Related