【发布时间】:2021-11-02 01:48:19
【问题描述】:
我有这样的字符串:
71,72,80,81,102,100
我想分隔每 2 个“数字”,所以:
71,72
80,81
102,100
我写了这个正则表达式:
(([0-9]{1,4}),([0-9]{1,4}))
突出显示我需要的组,除了“,”之间的逗号
在我的代码中,我使用的是dplyr
例子:
df_example <- tibble(Lotes= "LOT1,LOT2,LOT3",NoModuloPlastico = "71,72,80,81,102,100")
df_result_example <- df_example %>%
separate_rows(c(Lotes),sep=",") %>%
separate_rows(c(NoModuloPlastico),sep="(([0-9]{1,3}),([0-9]{1,3}))")
这意味着我真正需要的是用正则表达式突出显示每 2 个逗号,但我找不到方法。
我无法根据我的需要调整这些链接:
https://bedigit.com/blog/regex-how-to-match-everything-except-a-particular-pattern/
https://blog.codinghorror.com/excluding-matches-with-regular-expressions/
我得到了什么:
| Lotes | NoModuloPlastico |
|---|---|
| LOT1 | "" |
| LOT1 | "," |
| LOT1 | "," |
| LOT1 | "" |
| LOT2 | "" |
| LOT2 | "," |
| LOT2 | "," |
| LOT2 | "" |
| LOT3 | "" |
| LOT3 | "," |
| LOT3 | "," |
| LOT3 | "" |
我想要什么:
| Lotes | NoModuloPlastico |
|---|---|
| LOT1 | 71,72 |
| LOT2 | 80,81 |
| LOT3 | 102,100 |
【问题讨论】:
标签: r regex dataframe dplyr separator