在R中分隔两个人之间聊天的正则表达式

在R中分隔两个人之间聊天的正则表达式,r,regex,R,Regex,我有一个字符串: "玛丽g:你好,伊登医生,我能为你做些什么dany:你好,医生门户网站中有没有一个地方可以查看我目前有多少假期和病假时间?非常感谢你抽出时间。玛丽g:是的,你可以在你的安全人力资源页面的福利摘要下查看,首先,你必须在右边输入你的ssn的最后四个直接转到您的安全人力资源页面,在左侧会有一个链接列表选择福利摘要mary g:您也可以在每两周一次的工资存根上查看它dany:我想它可能在工资存根上,我似乎永远也找不到它:现在查看福利摘要dany:太好了,我看到这里有假期和病假来确认这是

我有一个字符串:

"玛丽g:你好,伊登医生,我能为你做些什么dany:你好,医生门户网站中有没有一个地方可以查看我目前有多少假期和病假时间?非常感谢你抽出时间。玛丽g:是的,你可以在你的安全人力资源页面的福利摘要下查看,首先,你必须在右边输入你的ssn的最后四个直接转到您的安全人力资源页面,在左侧会有一个链接列表选择福利摘要mary g:您也可以在每两周一次的工资存根上查看它dany:我想它可能在工资存根上,我似乎永远也找不到它:现在查看福利摘要dany:太好了,我看到这里有假期和病假来确认这是我目前的应计金额mary g:你必须在工资支票上选择quotprintviewquot选项,应计金额列在你的收入下mary g:很好,是的,没错mary g:这是你上一个工资期的应计金额mary g:所以目前它是准确的,直到dany:很好,非常感谢,注意到了关于printview dany:非常感谢您的帮助mary g:很高兴能为您提供任何其他帮助dany:再次感谢您mary g:祝您度过愉快的一天”

我想提取mary g写的所有聊天记录,我尝试了下面的正则表达式,但它只给出了第一行字符串

text\u agnt=gsub(“.*^[[:alpha:][^:][+:\\s*|\\s[A-Za-z]{3,10}:.*$”,“”,text)
输出:“你好,伊登博士,我能为您做些什么”


预期输出:mary g编写的所有文本在示例数据中,名称和冒号前有两个以上的空格字符。在这种情况下,您可以使用正向前瞻断言检查以下内容,即两个空格字符后跟一个
,至少有一个
[a-Za-z]
在其前面或字符串的结尾

\bmary\s+g:\s*(.*?)(?=\s{2}[^:]*[A-Za-z]:|$)


尝试使用捕获组
\bmary\s+g:\s*(.*?)
谢谢,您看到Dany为上述内容提取聊天记录的模式了吗?@Nishant这样?好的,这将适用于特定的名称,但我有多个不同名称的字符串,mary g和Dany只是一个示例,它可以是任何模式,但对于客户,名称的顺序将是相同的(名字和姓氏(一个字符),而dr.name将只包含名字)谢谢,我也能为随机名字做这件事,谢谢help@Nishant当然,您可以在您必须指定冒号前面允许的内容的模式中使用此选项*是一个非常广泛的匹配,匹配除
以外的任何字符:
此模式仅匹配由空格字符分隔的单词字符
\b\w+(?:\s+\w+*:\s*(*?)(=\s{2}[^::*[a-Za-z]:|$)
library(stringr)

txt <- "mary g: hello dr ydeen how can i help you   dany: hi is there a place within the physician portal to see how many vacation and sick hours i currently have thank you so much for your time    mary g: yes you can view that in your secure hr page under benefits summary first youll have to enter the last four of your ssn on the right hand side youll be directed to your secure hr page and on the left hand side there will be a list of links select benefits summary    mary g: you can also view it on each pay stub biweekly    dany: i thought it might be on the paystub i can never seem to find it there : checking out benefits summary now    dany: great i see vacation and sick time here to confirm this is how much i have currently accrued    mary g: youll have to select the quotprintviewquot option on the paycheck and the accruals are listed under your earnings    mary g: great yes thats correct    mary g: its your accruals as of the last pay period    mary g: so currently its accurate up until     dany: perfect thank you so much and noted regarding printview    dany: thanks so much for your help    mary g: my pleasure anything else i can help you with    dany: thats all thanks again    mary g: have a great day"
pattern <- "\\bmary\\s+g:\\s*(.*?)(?=\\s{2}[^:]*[A-Za-z]:|$)"
str_match_all(txt, pattern)[[1]][,2]
[1] "hello dr ydeen how can i help you"                                                                                                                                                                                                                                      
[2] "yes you can view that in your secure hr page under benefits summary first youll have to enter the last four of your ssn on the right hand side youll be directed to your secure hr page and on the left hand side there will be a list of links select benefits summary"
[3] "you can also view it on each pay stub biweekly"                                                                                                                                                                                                                         
[4] "youll have to select the quotprintviewquot option on the paycheck and the accruals are listed under your earnings"                                                                                                                                                      
[5] "great yes thats correct"                                                                                                                                                                                                                                                
[6] "its your accruals as of the last pay period"                                                                                                                                                                                                                            
[7] "so currently its accurate up until"                                                                                                                                                                                                                                     
[8] "my pleasure anything else i can help you with"                                                                                                                                                                                                                          
[9] "have a great day"