I have text in below format. I need to extract PolicyNumber and Code from these
"DUPLICATE x\nifa’-\nUPLICAT My Life\nInsurance\n02-01-2020\nMr SATHISH KUMAR T\nNo 77a Guru street Devi\nNagar Tambaram\nChennai 900083\n(M) 123456789\nPolicy Number : 0234567\nYour My Life Secured Income Insurance RP with Policy No. 0234567\nDear SATHISH KUMAR T,\nThank you for choosing My Life Insurance as your preferred life insurance solution provider. We welcome you to our\ngrowing happy family of over 1.5 million customers.\nWe are pleased to enclose your Policy Bond, which carries the following details of your My Life Insurance Policy:\nv_ Policy Schedule : Summary of key features of your My Life Insurance Policy\nv Premium Receipt : Acknowledgement of the first premium paid by you\nv_ Terms & Conditions : Detailed terms of your Policy contract with My Life Insurance\nv_ Service Options : Wide range of Policy servicing options that you can benefit from\nWe request you to carefully go through the information given in this document. You are also advised to keep the Policy\nBond with utmost care and safety.\nYou shall have a period of 15 days (30 days if the Policy is sourced through Distance Marketing# as provided in Distance\nMarketing Guidelines ) from the date of receipt of the Policy\ndocument to review the terms and conditions of this Policy and in case of disagreement with the said terms and\nconditions, an option to return the Policy to the Company for cancellation can be exercised stating the reasons for\nobjections. Upon such Free-Look cancellation, the Company shall return the Premium paid subject to deduction of a\nproportionate risk Premium for the period of insurance cover in addition to the stamp duty charges. All Benefits and rights\nunder this Policy shall immediately stand terminated on the cancellation of the Policy.\n#Distance Marketing includes solicitation through all modes other than in person.\nFor any assistance, please feel free to contact our customer service team at [email protected] or call our toll free number\n1800 345 6789.\nThank you for giving us the opportunity to help you prepare for a long and happy life.\nYours sincerely,\nAl j ‘Jo ft\nKumar Jean\nManaging Head & FEO\nMy Life Insurance Company Limited\n‘x Your Financial Advisor Contact Details\nAdvisor Name: Flarance V\nAdvisor Code: 78423368\nMobile/ Landline Number: (M) 1234563425\nVersion/My Life/PBD/N_RP/1.0\n"
I tried using below code
poln <- str_match(text, "Policy Number :\\s*(.*?)\\s*\nYour")
polnumb <- poln[,2]
advcd <- str_match(text, "Advisor Code:\\s*(.*?)\\s*Mobile")
advcode <- advcd[,2]
# Returns null
poln
[,1] [,2]
[1,] NA NA
It returns a NULL. Anything I missed ?
CodePudding user response:
You could use stringr
with look behind notation.
library(stringr)
policy_nr <- str_extract(txt,"(?<=Policy Number \\: )\\d ")
policy_nr
#> [1] "0234567"
advisor_code <- str_extract(txt,"(?<=Advisor Code\\: )\\d ")
advisor_code
#> [1] "78423368"
Created on 2021-12-02 by the reprex package (v2.0.1)