繁体   English   中英

使用正则表达式组合在Strsplit中保留分隔符

[英]Keep delimiter in Strsplit with regex combinations

我正在修改一些需要我使用strsplit组合regex函数的strsplit 我已经想出了如何分割我的字符串,但我正在努力应用这篇文章中关于保持分隔符的指导。

这是我正在抓取的字符串示例:

text<-c("This activity center is fun and helps give your birds exercise! With climbing ladders, a swing, tightrope and an assortment of engaging toys, the Activity Center has everything your bird needs to relieve stress and boredom all in one place. Relieves stress & boredom Durable & brightly colored wood Easy to clean bottom Simple installation & assemblyMaterial: WoodDimensions (Overall): 12.0 inches (H) x 15.0 inches (W) x 18.5 inches (L)Weight: 6.0 poundsHolds up to: 20.0 poundsIntended Pet Type: BirdCare and Cleaning: Hand washPet activity: ClimbTCIN: 16707835UPC: 030172025594Item Number (DPCI): 083-01-0246Report incorrect product information")

并且,这里是成功拆分字符串的代码,但修剪了分隔符:

strsplit(as.character(free_text), "[0-9](?=[AZ])|[az](?=[AZ])|[')'](?=[AZ])", perl=TRUE)

正如您将注意到的,我正在寻找以下地方:

  • 小写字母直接与大写字母相邻
  • 大写字母旁边的数字
  • 大写字母旁边的括号括号

不幸的是,下面的输出显示了我的代码的问题:

[1] "This activity center is fun and helps give your birds exercise! With climbing ladders, a swing, tightrope and an assortment of engaging toys, the Activity Center has everything your bird needs to relieve stress and boredom all in one place. Relieves stress & boredom Durable & brightly colored wood Easy to clean bottom Simple installation & assembl" [2] "Material: Woo"
[3] "Dimensions (Overall): 12.0 inches (H) x 15.0 inches (W) x 18.5 inches (L"
[4] "Weight: 6.0 pound"
[5] "Holds up to: 20.0 pound"
[6] "Intended Pet Type: Bir"
[7] "Care and Cleaning: Hand was"
[8] "Pet activity: Clim"
[9] "TCIN: 1670783"
[10] "UPC: 03017202559"
[11] "Item Number (DPCI): 083-01-024"
[12] "Report incorrect product information"

即,最后一个字母是从assemble [1]Wood [2]修剪的。 当你在寻找像我这样的正则表达式组合时,如何保留分隔符?

您可以将正则表达式中的消费模式放入lookbehinds:

> text<-c("This activity center is fun and helps give your birds exercise! With climbing ladders, a swing, tightrope and an assortment of engaging toys, the Activity Center has everything your bird needs to relieve stress and boredom all in one place. Relieves stress & boredom Durable & brightly colored wood Easy to clean bottom Simple installation & assemblyMaterial: WoodDimensions (Overall): 12.0 inches (H) x 15.0 inches (W) x 18.5 inches (L)Weight: 6.0 poundsHolds up to: 20.0 poundsIntended Pet Type: BirdCare and Cleaning: Hand washPet activity: ClimbTCIN: 16707835UPC: 030172025594Item Number (DPCI): 083-01-0246Report incorrect product information")
> strsplit(text, "(?<=[0-9])(?=[A-Z])|(?<=[a-z])(?=[A-Z])|(?<=\\))(?=[A-Z])", perl=TRUE)
[[1]]
 [1] "This activity center is fun and helps give your birds exercise! With climbing ladders, a swing, tightrope and an assortment of engaging toys, the Activity Center has everything your bird needs to relieve stress and boredom all in one place. Relieves stress & boredom Durable & brightly colored wood Easy to clean bottom Simple installation & assembly"
 [2] "Material: Wood"                                                                                                                                                                                                                                                                                                                                                
 [3] "Dimensions (Overall): 12.0 inches (H) x 15.0 inches (W) x 18.5 inches (L)"                                                                                                                                                                                                                                                                                     
 [4] "Weight: 6.0 pounds"                                                                                                                                                                                                                                                                                                                                            
 [5] "Holds up to: 20.0 pounds"                                                                                                                                                                                                                                                                                                                                      
 [6] "Intended Pet Type: Bird"                                                                                                                                                                                                                                                                                                                                       
 [7] "Care and Cleaning: Hand wash"                                                                                                                                                                                                                                                                                                                                  
 [8] "Pet activity: Climb"                                                                                                                                                                                                                                                                                                                                           
 [9] "TCIN: 16707835"                                                                                                                                                                                                                                                                                                                                                
[10] "UPC: 030172025594"                                                                                                                                                                                                                                                                                                                                             
[11] "Item Number (DPCI): 083-01-0246"                                                                                                                                                                                                                                                                                                                               
[12] "Report incorrect product information"     

查看正则表达式演示在线R演示

[0-9]转换为(?<=[0-9])[az]现在是(?<=[az])[')']现在是(?<=\\))

请注意, (?<=...)是一个正向的lookbehind ,它匹配一个字符串中的位置,该位置紧跟在lookbehind中定义的某个模式之前。

暂无
暂无

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

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