简体   繁体   English

R strsplit使用正则表达式

[英]R strsplit using Regex

I want to use R to split some chat messages, here is an example: 我想用R来分割一些聊天消息,这里有一个例子:

example <- "[29.01.18, 23:33] Alice: Ist das hier ein Chatverlauf?\n[29.01.18, 23:45] Bob: Ja ist es!\n[29.01.18, 23:45] Bob: Der ist dazu da die funktionsweise des Parsers zu demonstrieren\n[29.01.18, 23:46] Alice: ‎PTT-20180129-WA0025.opus (Datei angehängt)\n[29.01.18, 23:46] Bob: Ah, er kann also auch erkennen ob Voicemails gesendet wurden!\n[29.01.18, 23:46] Bob: Das ist praktisch!\n[29.01.18, 23:47] Bob: Oder?\n[29.01.18, 23:47] Alice: ja |Emoji_Grinning_Face_With_Smiling_Eyes| \n[29.01.18, 23:47] Alice: und Emojis gehen auch!\n[29.01.18, 23:47] Bob: Was ist mit normalen Smilies?\n[29.01.18, 23:49] Alice: ‎Keine Ahnung, lass uns das doch mal ausprobieren\n[29.01.18, 23:50] Bob: Alles klar :) :D\n[29.01.18, 23:51] Alice: Scheint zu funktionieren!:P\n[29.01.18, 23:51] Bob: Meinst du, dass URLS auch erkannt werden?\n[29.01.18, 23:52] Bob: ‎Schick doch mal eine zum ausprobieren!\n[29.01.18, 23:53] Alice: https://github.com/JuKo007\n[29.01.18, 23:58] Alice: ‎Scheint zu funktionieren!\n[29.01.18, 23:59] Alice: Sehr schön!\n[30.01.18, 00:00] Alice: Damit sollten sich WhatsApp Verläufe besser quantifizieren lassen!\n[30.01.18, 00:02] Bob: ‎Alles klar, los gehts  |Emoji_Relieved_Face| \n"

Basically, I want to split the string right in front of the date-time indicator in the brackets, here is what I tried so far: 基本上,我想在括号中的日期时间指示器前面分割字符串,这是我到目前为止所尝试的:

  # Cutting the textblock into individual messages
  chat <- strsplit(example,"(?=\\[\\d\\d.\\d\\d.\\d\\d, \\d\\d:\\d\\d\\])",perl=TRUE)
  chat <- unlist(chat)

The weird thing is, that in the output, it seems that the split occurs after the first square bracket, not in front: 奇怪的是,在输出中,似乎分裂发生第一个方括号之后,而不是在前面:

 [1] "["                                                                                           
 [2] "29.01.18, 23:33] Alice: Ist das hier ein Chatverlauf?\n"                                     
 [3] "["                                                                                           
 [4] "29.01.18, 23:45] Bob: Ja ist es!\n"                                                          
 [5] "["                                                                                           
 [6] "29.01.18, 23:45] Bob: Der ist dazu da die funktionsweise des Parsers zu demonstrieren\n"     
 [7] "["                                                                                           
 [8] "29.01.18, 23:46] Alice: ‎PTT-20180129-WA0025.opus (Datei angehängt)\n"                        
 [9] "["                                                                                           
[10] "29.01.18, 23:46] Bob: Ah, er kann also auch erkennen ob Voicemails gesendet wurden!\n"       
[11] "["                                                                                           
[12] "29.01.18, 23:46] Bob: Das ist praktisch!\n"                                                  
[13] "["                                                                                           
[14] "29.01.18, 23:47] Bob: Oder?\n"                                                               
[15] "["                                                                                           
[16] "29.01.18, 23:47] Alice: ja |Emoji_Grinning_Face_With_Smiling_Eyes| \n"                       
[17] "["                                                                                           
[18] "29.01.18, 23:47] Alice: und Emojis gehen auch!\n"                                            
[19] "["                                                                                           
[20] "29.01.18, 23:47] Bob: Was ist mit normalen Smilies?\n"                                       
[21] "["                                                                                           
[22] "29.01.18, 23:49] Alice: ‎Keine Ahnung, lass uns das doch mal ausprobieren\n"                  
[23] "["                                                                                           
[24] "29.01.18, 23:50] Bob: Alles klar :) :D\n"                                                    
[25] "["                                                                                           
[26] "29.01.18, 23:51] Alice: Scheint zu funktionieren!:P\n"                                       
[27] "["                                                                                           
[28] "29.01.18, 23:51] Bob: Meinst du, dass URLS auch erkannt werden?\n"                           
[29] "["                                                                                           
[30] "29.01.18, 23:52] Bob: ‎Schick doch mal eine zum ausprobieren!\n"                              
[31] "["                                                                                           
[32] "29.01.18, 23:53] Alice: https://github.com/JuKo007\n"                                        
[33] "["                                                                                           
[34] "29.01.18, 23:58] Alice: ‎Scheint zu funktionieren!\n"                                         
[35] "["                                                                                           
[36] "29.01.18, 23:59] Alice: Sehr schön!\n"                                                       
[37] "["                                                                                           
[38] "30.01.18, 00:00] Alice: Damit sollten sich WhatsApp Verläufe besser quantifizieren lassen!\n"
[39] "["                                                                                           
[40] "30.01.18, 00:02] Bob: ‎Alles klar, los gehts  |Emoji_Relieved_Face| \n" 

When I try to test the Regex pattern online or use it in python, it works just as intended, so to me it seems that this is a feature of the strsplit function? 当我尝试在线测试Regex模式或在python中使用它时,它的工作方式与预期一致,所以对我来说这似乎是strsplit函数的一个特性? Any recommendation on how to change my R code to make this work are very welcome! 任何有关如何更改我的R代码以使其工作的建议都非常受欢迎! I know that it would be easy to just paste this output back together to get my desired output but I would really like to understand whats going on with strsplit and do it properly instead of patching it back together. 我知道将这个输出重新粘贴在一起以获得我想要的输出会很容易但我真的很想了解strsplit正在进行什么,并且正确地执行它而不是将它重新打包在一起。 What I want is: 我想要的是:

 [1] "[29.01.18, 23:33] Alice: Ist das hier ein Chatverlauf?\n"                                                                                                                           
 [2] "[29.01.18, 23:45] Bob: Ja ist es!\n"                                                                                                                                                  
 [3] "[29.01.18, 23:45] Bob: Der ist dazu da die funktionsweise des Parsers zu demonstrieren\n"                                                                                         
 [4] "[29.01.18, 23:46] Alice: ‎PTT-20180129-WA0025.opus (Datei angehängt)\n"                                                                                                      
[5] "[29.01.18, 23:46] Bob: Ah, er kann also auch erkennen ob Voicemails gesendet wurden!\n"                                                                                          
[6] "[29.01.18, 23:46] Bob: Das ist praktisch!\n"                                                                                                                                    
[7] "[29.01.18, 23:47] Bob: Oder?\n"                                                                                                                                                   
[8] "[29.01.18, 23:47] Alice: ja |Emoji_Grinning_Face_With_Smiling_Eyes| \n"                                                                                                            
[9] "[29.01.18, 23:47] Alice: und Emojis gehen auch!\n"                                                                                                                          
[10] "[29.01.18, 23:47] Bob: Was ist mit normalen Smilies?\n"                                                                                                                         
[11] "[29.01.18, 23:49] Alice: ‎Keine Ahnung, lass uns das doch mal ausprobieren\n"                                                                                                    
[12] "[29.01.18, 23:50] Bob: Alles klar :) :D\n"                                                                                                                                       
[13] "[29.01.18, 23:51] Alice: Scheint zu funktionieren!:P\n"                                                                                                                        
[14] "[29.01.18, 23:51] Bob: Meinst du, dass URLS auch erkannt werden?"                                                                                                             
[15] "[29.01.18, 23:52] Bob: ‎Schick doch mal eine zum ausprobieren!\n"                                                                                                                       
[16] "[29.01.18, 23:53] Alice: https://github.com/JuKo007\n"                                                                                                                                  
[17] "[29.01.18, 23:58] Alice: ‎Scheint zu funktionieren!\n"                                                                                                                                  
[18] "[29.01.18, 23:59] Alice: Sehr schön!\n"                                                                                                                                                
[19] "[30.01.18, 00:00] Alice: Damit sollten sich WhatsApp Verläufe besser quantifizieren lassen!\n"                                                                                           
[20] "[30.01.18, 00:02] Bob: ‎Alles klar, los gehts  |Emoji_Relieved_Face| \n" 

You could add a negative lookahead (?!^) to assert not the start of the string. 你可以添加一个负向前瞻(?!^)来断言不是字符串的开头。

Your updated line might look like: 您更新的行可能如下所示:

chat <- strsplit(example,"(?!^)(?=\\[\\d\\d.\\d\\d.\\d\\d, \\d\\d:\\d\\d\\])",perl=TRUE)

R demo R演示

Result 结果

 [1] "[29.01.18, 23:33] Alice: Ist das hier ein Chatverlauf?\n"                                     
 [2] "[29.01.18, 23:45] Bob: Ja ist es!\n"                                                          
 [3] "[29.01.18, 23:45] Bob: Der ist dazu da die funktionsweise des Parsers zu demonstrieren\n"     
 [4] "[29.01.18, 23:46] Alice: ‎PTT-20180129-WA0025.opus (Datei angehängt)\n"                        
 [5] "[29.01.18, 23:46] Bob: Ah, er kann also auch erkennen ob Voicemails gesendet wurden!\n"       
 [6] "[29.01.18, 23:46] Bob: Das ist praktisch!\n"                                                  
 [7] "[29.01.18, 23:47] Bob: Oder?\n"                                                               
 [8] "[29.01.18, 23:47] Alice: ja |Emoji_Grinning_Face_With_Smiling_Eyes| \n"                       
 [9] "[29.01.18, 23:47] Alice: und Emojis gehen auch!\n"                                            
[10] "[29.01.18, 23:47] Bob: Was ist mit normalen Smilies?\n"                                       
[11] "[29.01.18, 23:49] Alice: ‎Keine Ahnung, lass uns das doch mal ausprobieren\n"                  
[12] "[29.01.18, 23:50] Bob: Alles klar :) :D\n"                                                    
[13] "[29.01.18, 23:51] Alice: Scheint zu funktionieren!:P\n"                                       
[14] "[29.01.18, 23:51] Bob: Meinst du, dass URLS auch erkannt werden?\n"                           
[15] "[29.01.18, 23:52] Bob: ‎Schick doch mal eine zum ausprobieren!\n"                              
[16] "[29.01.18, 23:53] Alice: https://github.com/JuKo007\n"                                        
[17] "[29.01.18, 23:58] Alice: ‎Scheint zu funktionieren!\n"                                         
[18] "[29.01.18, 23:59] Alice: Sehr schön!\n"                                                       
[19] "[30.01.18, 00:00] Alice: Damit sollten sich WhatsApp Verläufe besser quantifizieren lassen!\n"
[20] "[30.01.18, 00:02] Bob: ‎Alles klar, los gehts  |Emoji_Relieved_Face| \n"    

You can use stringi and extract the info you want by slightly modifying the end of your pattern (ie, matching everything until the next [ ). 您可以使用stringi并通过稍微修改模式的结尾来提取所需的信息(即,匹配所有内容,直到下一个[ )。 You could include more of your pattern to ensure there aren't any false-matches but this should get your started. 你可以包含更多你的模式,以确保没有任何错误匹配,但这应该让你开始。 Good luck! 祝好运!

library(stringi)

stri_extract_all(example, regex = "\\[\\d\\d.\\d\\d.\\d\\d, \\d\\d:\\d\\d\\][^\\[]*")
[[1]]
 [1] "[29.01.18, 23:33] Alice: Ist das hier ein Chatverlauf?\n"                                     
 [2] "[29.01.18, 23:45] Bob: Ja ist es!\n"                                                          
 [3] "[29.01.18, 23:45] Bob: Der ist dazu da die funktionsweise des Parsers zu demonstrieren\n"     
 [4] "[29.01.18, 23:46] Alice: \016PTT-20180129-WA0025.opus (Datei angehängt)\n"                    
 [5] "[29.01.18, 23:46] Bob: Ah, er kann also auch erkennen ob Voicemails gesendet wurden!\n"       
 [6] "[29.01.18, 23:46] Bob: Das ist praktisch!\n"                                                  
 [7] "[29.01.18, 23:47] Bob: Oder?\n"                                                               
 [8] "[29.01.18, 23:47] Alice: ja |Emoji_Grinning_Face_With_Smiling_Eyes| \n"                       
 [9] "[29.01.18, 23:47] Alice: und Emojis gehen auch!\n"                                            
[10] "[29.01.18, 23:47] Bob: Was ist mit normalen Smilies?\n"                                       
[11] "[29.01.18, 23:49] Alice: \016Keine Ahnung, lass uns das doch mal ausprobieren\n"              
[12] "[29.01.18, 23:50] Bob: Alles klar :) :D\n"                                                    
[13] "[29.01.18, 23:51] Alice: Scheint zu funktionieren!:P\n"                                       
[14] "[29.01.18, 23:51] Bob: Meinst du, dass URLS auch erkannt werden?\n"                           
[15] "[29.01.18, 23:52] Bob: \016Schick doch mal eine zum ausprobieren!\n"                          
[16] "[29.01.18, 23:53] Alice: https://github.com/JuKo007\n"                                        
[17] "[29.01.18, 23:58] Alice: \016Scheint zu funktionieren!\n"                                     
[18] "[29.01.18, 23:59] Alice: Sehr schön!\n"                                                       
[19] "[30.01.18, 00:00] Alice: Damit sollten sich WhatsApp Verläufe besser quantifizieren lassen!\n"
[20] "[30.01.18, 00:02] Bob: \016Alles klar, los gehts  |Emoji_Relieved_Face| \n"   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM