首页 > 解决方案 > 使用 R 获取匹配字符串的文本之前(文本分析)

问题描述

我只想在文本匹配字符串之前获取文本。

例如:

text :'Hello Ala, \r\r\n\r\r\nPlease perform receipting on line 12 of PO 78 so that the Invoices WB381 & AU47  will be processed. \r\r\n\r\r\nShould you have any further questions please call us on 76 or email us quoting the reference number in the subject line of the email. We would be more than happy to help you.\r\r\n\r\r\nThanks & Regards,\r\r\nMathen\r\r\n\r\r\nTo assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training and quality reviews. For further information please see our privacy policy on R80.com\r\r\n------------------------------------------------------------------------------------------------\r\r\nTo: a.wall@xxx.xom\r\r\nFrom: procurementhelp@xxx.xom\r\r\nCC: \r\r\nDate: 2019-11-12 04:45:41\r\r\nSubject: RE:RE: RE:R80 78 On-hold Invoices Assistance Required\r\r\n\r\r\nHello Alastair, \r\r\n\r\r\nPlease be informed that our support team is working on the query. \r\r\n\r\r\nWe will get back to you as soon as we get a response from the team. \r\r\n\r\r\nShould you have any further questions please call us on 76 or email us quoting the reference number in the subject line of the email. We would be more than happy to help you.\r\r\n\r\r\nThanks & Regards,\r\r\nMathen K Mathen\r\r\nGroup finance Consultant\r\r\nR80 Airways Limited ;\r\r\nP. 90\r\r\nE. procurementhelp@xxx.xom<mailto:procurementhelp@xxx.xom>\r\r\nR80.com | \r\r\n\r\r\nTo assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training and quality reviews. For further information please see our privacy policy on R80.com\r\r\n------------------------------------------------------------------------------------------------\r\r\nTo: procurementhelp@ps.xxx.xom\r\r\nFrom: a.wall@xxx.xom\r\r\nCC: \r\r\nDate: 2019-11-07 23:18:59\r\r\nSubject: RE:RE: RE:R80 78 On-hold Invoices Assistance Required\r\r\n\r\r\nHi Team,\r\r\nI have never heard back on this query and there are now several invoices on hold that I cannot release.\r\r\nWB381\r\r\nWBFI420019397\r\r\nUPFI520009670\r\r\nAUNI220007659\r\r\nAUNI220007658\r\r\nAUNI220006740\r\r\nWBFI420015784\r\r\nUPFI520008050\r\r\nAUNI220006739\r\r\nAUNI220006741\r\r\nUPFI520006663\r\r\n\r\r\n\r\r\nKind Regards,\r\r\nAlastair Wall\r\r\nDomain Operations Lead\r\r\nR80 Engineering & Flight Operations\r\r\n\r\r\nR80 Airways Limited ??\r\r\n10 Bourke Road, Mascot NSW 2020\r\r\nP. +61 2 9691 1491 (ext. 21491)\r\r\nM. +61 415 691 953\r\r\nE. a.wall@xxx.xom<mailto:a.wall@xxx.xom>\r\r\n\r\r\nR80.com>\r\r\n\r\r\nOur team will be in contact with you within the next 48 hours. If this has not occurred, please call us on 78 / 79 or email us quoting the reference number for any further questions.\r\r\n\r\r\nThanks,\r\r\nMathen K Mathen\r\r\nGroup Finance Consultant\r\r\nR80 Airways Limited\r\r\nP. 78/ 79\r\r\nE. supplierhelp@xxx.xom<mailto:supplierhelp@xxx.xom> /procurementhelp@xxx.xom<mailto:/procurementhelp@xxx.xom>\r\r\n\r\r\nR80.com | facebook.com/R80 | twitter@R80 | youtube.com/R80\r\r\n\r\r\nTo assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training.\r\r\n------------------------------------------------------------------------------------------------\r\r\nTo: ProcurementHelp@xxx.xom<mailto:ProcurementHelp@xxx.xom>\r\r\nFrom: a.wall@xxx.xom<mailto:a.wall@xxx.xom>\r\r\nCC:\r\r\nDate: 2019-10-22 02:39:07\r\r\nSubject: RE:78 On-hold Invoices Assistance Required\r\r\n\r\r\nHi team,\r\r\nWould you please assist with releasing the below pending invoices. The PO 78 is receipted above the invoiced amount so these should not be on-hold.\r\r\n\r\r\nUPFI520008050\r\r\n26-Sep-2019\r\r\nStandard\r\r\nTata Consultancy Svcs Ltd\r\r\nTCS BASELINE\r\r\nAUD\r\r\n20,000.00\r\r\n20,000.00\r\r\nIn Process - Pending Payment\r\r\nMax Amt Ord\r\r\nNot Paid\r\r\n10-NOV-2019\r\r\n78\r\r\n[cid:image001.gif@01D588DD.AF8F1F00]\r\r\nAUNI220006740\r\r\n26-Sep-2019\r\r\nStandard\r\r\nTata Consultancy Svcs Ltd\r\r\nTCS BASELINE\r\r\nAUD\r\r\n5,500.00\r\r\n5,500.00\r\r\nIn Process - Pending Payment\r\r\nMax Amt Ord\r\r\nNot Paid\r\r\n10-NOV-2019\r\r\n78\r\r\n[cid:image001.gif@01D588DD.AF8F1F00]\r\r\nWBFI420015784\r\r\n26-Sep-2019\r\r\nStandard\r\r\nTata Consultancy Svcs Ltd\r\r\nTCS BASELINE\r\r\nAUD\r\r\n1,400.00\r\r\n1,400.00\r\r\nIn Process - Pending Payment\r\r\nMax Amt Ord\r\r\nNot Paid\r\r\n10-NOV-2019\r\r\n78\r\r\n\r\r\nRef:MSG24764190\r\r\n'

我想'\r\r\n------------------------------------------------------------------------------------------------\r\r\n'从文本中找到模式。然后只想从第一个匹配的文本中获取数据

输出:

Hello Ala, 

Please perform receipting on line 12 of PO 78 so that the Invoices WB381 & AU47  will be processed. 

Should you have any further questions please call us on 76 or email us quoting the reference number in the subject line of the email. We would be more than happy to help you

Thanks & Regards,
Mathen 

To assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training and quality reviews. For further information please see our privacy policy on R80.com

标签: rregexdataframe

解决方案


out <- gsub("[\r\n]+-{96}[\r\n]+.*", "", text)

专门寻找 96 似乎很奇怪,寻找(比如)“50 或更多”可能是可以接受的,在这种情况下更改{96}{50,}. 只是一个想法。

结果:

Hello Ala, 

Please perform receipting on line 12 of PO 78 so that the Invoices WB381 & AU47  will be processed. 

Should you have any further questions please call us on 76 or email us quoting the reference number in the subject line of the email. We would be more than happy to help you.

Thanks & Regards,
Mathen

To assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training and quality reviews. For further information please see our privacy policy on R80.com 

解释:

  • [\r\n]+一个或多个来自一组\ror \n;我承认我将您的模式简化为“一个或多个”而不是确切的"\r\r\n\r\r\n",部分原因是\rand的顺序\n违反了 CRLF 或其他处理,这表明计数中有一些不受控制的歧义;
  • -{96}正好 96 个破折号;当使用{m,n}需要至少m且不超过n出现前一个字符/组的重复器时,如果,n省略则表示精确计数;
  • 尾随.*从字符串中删除所有其他内容

增强:通常当我看到这样的问题时,下一个问题是“我如何让第二封电子邮件远离链”?

我们可以根据同一种线来分割字符串,而不是剪掉字符串:

emails <- strsplit(text, "[-\n\r]+-{90,}[\r\n]+")[[1]]
emails
# [1] "Hello Ala, \r\r\n\r\r\nPlease perform receipting on line 12 of PO 78 so that the Invoices WB381 & AU47  will be processed. \r\r\n\r\r\nShould you have any further questions please call us on 76 or email us quoting the reference number in the subject line of the email. We would be more than happy to help you.\r\r\n\r\r\nThanks & Regards,\r\r\nMathen\r\r\n\r\r\nTo assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training and quality reviews. For further information please see our privacy policy on R80.com"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
# [2] "To: a.wall@xxx.xom\r\r\nFrom: procurementhelp@xxx.xom\r\r\nCC: \r\r\nDate: 2019-11-12 04:45:41\r\r\nSubject: RE:RE: RE:R80 78 On-hold Invoices Assistance Required\r\r\n\r\r\nHello Alastair, \r\r\n\r\r\nPlease be informed that our support team is working on the query. \r\r\n\r\r\nWe will get back to you as soon as we get a response from the team. \r\r\n\r\r\nShould you have any further questions please call us on 76 or email us quoting the reference number in the subject line of the email. We would be more than happy to help you.\r\r\n\r\r\nThanks & Regards,\r\r\nMathen K Mathen\r\r\nGroup finance Consultant\r\r\nR80 Airways Limited ;\r\r\nP. 90\r\r\nE. procurementhelp@xxx.xom<mailto:procurementhelp@xxx.xom>\r\r\nR80.com | \r\r\n\r\r\nTo assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training and quality reviews. For further information please see our privacy policy on R80.com"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
# [3] "To: procurementhelp@ps.xxx.xom\r\r\nFrom: a.wall@xxx.xom\r\r\nCC: \r\r\nDate: 2019-11-07 23:18:59\r\r\nSubject: RE:RE: RE:R80 78 On-hold Invoices Assistance Required\r\r\n\r\r\nHi Team,\r\r\nI have never heard back on this query and there are now several invoices on hold that I cannot release.\r\r\nWB381\r\r\nWBFI420019397\r\r\nUPFI520009670\r\r\nAUNI220007659\r\r\nAUNI220007658\r\r\nAUNI220006740\r\r\nWBFI420015784\r\r\nUPFI520008050\r\r\nAUNI220006739\r\r\nAUNI220006741\r\r\nUPFI520006663\r\r\n\r\r\n\r\r\nKind Regards,\r\r\nAlastair Wall\r\r\nDomain Operations Lead\r\r\nR80 Engineering & Flight Operations\r\r\n\r\r\nR80 Airways Limited ??\r\r\n10 Bourke Road, Mascot NSW 2020\r\r\nP. +61 2 9691 1491 (ext. 21491)\r\r\nM. +61 415 691 953\r\r\nE. a.wall@xxx.xom<mailto:a.wall@xxx.xom>\r\r\n\r\r\nR80.com>\r\r\n\r\r\nOur team will be in contact with you within the next 48 hours. If this has not occurred, please call us on 78 / 79 or email us quoting the reference number for any further questions.\r\r\n\r\r\nThanks,\r\r\nMathen K Mathen\r\r\nGroup Finance Consultant\r\r\nR80 Airways Limited\r\r\nP. 78/ 79\r\r\nE. supplierhelp@xxx.xom<mailto:supplierhelp@xxx.xom> /procurementhelp@xxx.xom<mailto:/procurementhelp@xxx.xom>\r\r\n\r\r\nR80.com | facebook.com/R80 | twitter@R80 | youtube.com/R80\r\r\n\r\r\nTo assist with your enquiry, our agents may record some personal data in our Ticketing system. This includes your full name, phone number and email address, you can request to modify this at any time. Recorded calls are stored for 2 months and used solely for the purposes of process improvement, agent training."
# [4] "To: ProcurementHelp@xxx.xom<mailto:ProcurementHelp@xxx.xom>\r\r\nFrom: a.wall@xxx.xom<mailto:a.wall@xxx.xom>\r\r\nCC:\r\r\nDate: 2019-10-22 02:39:07\r\r\nSubject: RE:78 On-hold Invoices Assistance Required\r\r\n\r\r\nHi team,\r\r\nWould you please assist with releasing the below pending invoices. The PO 78 is receipted above the invoiced amount so these should not be on-hold.\r\r\n\r\r\nUPFI520008050\r\r\n26-Sep-2019\r\r\nStandard\r\r\nTata Consultancy Svcs Ltd\r\r\nTCS BASELINE\r\r\nAUD\r\r\n20,000.00\r\r\n20,000.00\r\r\nIn Process - Pending Payment\r\r\nMax Amt Ord\r\r\nNot Paid\r\r\n10-NOV-2019\r\r\n78\r\r\n[cid:image001.gif@01D588DD.AF8F1F00]\r\r\nAUNI220006740\r\r\n26-Sep-2019\r\r\nStandard\r\r\nTata Consultancy Svcs Ltd\r\r\nTCS BASELINE\r\r\nAUD\r\r\n5,500.00\r\r\n5,500.00\r\r\nIn Process - Pending Payment\r\r\nMax Amt Ord\r\r\nNot Paid\r\r\n10-NOV-2019\r\r\n78\r\r\n[cid:image001.gif@01D588DD.AF8F1F00]\r\r\nWBFI420015784\r\r\n26-Sep-2019\r\r\nStandard\r\r\nTata Consultancy Svcs Ltd\r\r\nTCS BASELINE\r\r\nAUD\r\r\n1,400.00\r\r\n1,400.00\r\r\nIn Process - Pending Payment\r\r\nMax Amt Ord\r\r\nNot Paid\r\r\n10-NOV-2019\r\r\n78\r\r\n\r\r\nRef:MSG24764190\r\r\n"                                                                                                                                                                                                                                                                                                                                                                                                                                                             

推荐阅读