提取重复字符

Extracting repeated characters

我正在尝试提取艺术家和标题名称。然而,它有点复杂。 这是列表;

nlist <- c(
"Lil' SlimLil' Slim feat. PxMxWxPxMxWx Where Your Ward At!!",               
"I Like It (Mannie Fresh Style)I Like It (Mannie Fresh Style)Ms. Tee",
"Bella VistaBella Vista Mister Wong",
"Tom WareTom WareChina Town",                                        
"Race 'N RhythmRace 'N Rhythm Teenage Girls",                                    
"Ronald MarquisseRonald MarquisseElectro Link 7",
"PleasurePleasure Thoughts Of Old Flames",
"OM, OM, Dom Um RomaoDom Um Romao Chipero",
"HookfaceHookface4 07 181221"
)

这是字符串中的模式。

描述:

1和8太难了,我没法解决。但是下面的 2 到 7 个代码解决了我的问题。

title = str_trim(gsub('(.+?)\1','', nlist))
artist = re.match('(.+?)\1', nlist)[,2]
data = cbind(title,artist);data

这里是上述代码的输出。

     title                                     artist                          
[1,] "feat. PxMxWxPxMxWx Where Your Ward At!!" "Lil' Slim"                     
[2,] "Ms. Tee"                                 "I Like It (Mannie Fresh Style)"
[3,] "Mister Wong"                             "Bella Vista"                   
[4,] "China Town"                              "Tom Ware"                      
[5,] "Teenage Girls"                           "Race 'N Rhythm"                
[6,] "Electro Link 7"                          "Ronald Marquisse"              
[7,] "Thoughts Of Old Flames"                  "Pleasure"                      
[8,] "Chipero"                                 "OM, "  
[9,] "4 07 181221"                             "Hookeface"   

问题:当有“壮举”时。或“,”在字符串中,切割字符串的重复序列。 问题:我怎样才能像下面这样提取真正的艺术家名字?

我的预期结果在这里(检查 1 和 8);

     title                                     artist                          
[1,] "Where Your Ward At!!"                    "Lil' Slim feat. PxMxWx"                     
[2,] "Ms. Tee"                                 "I Like It (Mannie Fresh Style)"
[3,] "Mister Wong"                             "Bella Vista"                   
[4,] "China Town"                              "Tom Ware"                      
[5,] "Teenage Girls"                           "Race 'N Rhythm"                
[6,] "Electro Link 7"                          "Ronald Marquisse"              
[7,] "Thoughts Of Old Flames"                  "Pleasure"                      
[8,] "Chipero"                                 "OM, Dom Um Romao"                             
[9,] "4 07 181221"                             "Hookeface"                           

谢谢...

您可以使用以下方式获取艺术家:

artist <- sub("^(?:.*?\b(.+?)\1(?=\b|\p{Lu}))*\s*(.*)", "\2", nlist, perl=TRUE)
## => [1] "Where Your Ward At!!"   "Ms. Tee"                "Mister Wong"           
## => [4] "China Town"             "Teenage Girls"          "Electro Link 7"        
## => [7] "Thoughts Of Old Flames" "Chipero"  

您可以使用以下代码获取标题:

rx <- "^(?:.*?\b(.+?)\1(?=\b|\p{Lu}))*"
titles <- regmatches(nlist, regexpr(rx, nlist, perl=TRUE))
titles <- gsub("(.+?)\1", "\1", titles, perl=TRUE)
## => [1] "Lil' Slim feat. PxMxWx"        
## => [2] "I Like It (Mannie Fresh Style)"
## => [3] "Bella Vista"                   
## => [4] "Tom Ware"                      
## => [5] "Race 'N Rhythm"                
## => [6] "Ronald Marquisse"              
## => [7] "Pleasure"                      
## => [8] "OM, Dom Um Romao"

regex demo详情:

  • ^ - 字符串开头
  • (?:.*?\b(.+?)(?=\b|\p{Lu}))* - 零次或多次出现
    • .*? - 除换行字符外的任何零个或多个字符,尽可能少
    • \b - 单词边界
    • (.+?) - 第 1 组:除换行字符外的任何一个或多个字符,尽可能少
    • </code> - 与第 1 组相同的值</li> <li><code>(?=\b|\p{Lu}) - 下一个必须是单词边界或大写字母
  • \s* - 零个或多个空格
  • (.*) - 第 2 组 ():该行的其余部分。

参见R demo online

也许以下提取了您想要的内容。我删除所有内容和最后一次重复并将其存储在 title 中。为了获得艺术家,我使用 substr 删除了先前找到的 title 的长度,然后使用 gsub(.{2,})\1 删除了艺术家的重复,但这也会删除重复在连词 .

title <- sub(".*(.{2,})\1\s*", "", nlist)
artist <- trimws(gsub("(.{2,})\1", "\1"
              , substr(nlist, 1, nchar(nlist) - nchar(title)), perl=TRUE))
cbind(title,artist)
#      title                    artist                          
# [1,] "Where Your Ward At!!"   "Lil' Slim feat. PxMxWx"        
# [2,] "Ms. Tee"                "I Like It (Mannie Fresh Style)"
# [3,] "Mister Wong"            "Bella Vista"                   
# [4,] "China Town"             "Tom Ware"                      
# [5,] "Teenage Girls"          "Race 'N Rhythm"                
# [6,] "Electro Link 7"         "Ronald Marquisse"              
# [7,] "Thoughts Of Old Flames" "Pleasure"                      
# [8,] "Chipero"                "OM, Dom Um Romao"              
# [9,] "4 07 181221"            "Hookface"                      

另一种方式可能是:

x <- sub("^(.*)\1\s*", "", nlist)     #Remove the first repetition of artist
title <- sub(".*?(.{2,})\1\s*", "", x) #Remove Conjunction and repetition of Artist if there is one
artist <- trimws(gsub("(.{2,})\1", "\1"
              , substr(nlist, 1, nchar(nlist) - nchar(title)), perl=TRUE))
cbind(title,artist)
#      title                    artist                          
# [1,] "Where Your Ward At!!"   "Lil' Slim feat. PxMxWx"        
# [2,] "Ms. Tee"                "I Like It (Mannie Fresh Style)"
# [3,] "Mister Wong"            "Bella Vista"                   
# [4,] "China Town"             "Tom Ware"                      
# [5,] "Teenage Girls"          "Race 'N Rhythm"                
# [6,] "Electro Link 7"         "Ronald Marquisse"              
# [7,] "Thoughts Of Old Flames" "Pleasure"                      
# [8,] "Chipero"                "OM, Dom Um Romao"              
# [9,] "4 07 181221"            "Hookface"