R中的正则表达式检测单个数字和两个数字保持分隔符

Regex expression in R to detect single digit and two digits keeping the delimiters

我正在尝试使用 tidy R 包将行拆分为多行。

这是我数据集中的一个单元格

column 1
 1. a
 2. b
33. c

df = separate_rows(df,`column 1`, sep = "(?=\d[\.]\s)"

当我使用上面的代码时:

我明白了

Actual Output  |  Desired Output
1. a           |      1. a
2. b           |      2. b  
3              |     33. c
3. c           |

您可以使用

separate_rows(df,`column 1`, sep = "(?m)(?!\A)(?=^\d+\.\s)")

正则表达式详细信息

  • (?m) - ^ 现在匹配行首位置
  • (?!\A) - 在字符串开头时匹配失败的否定前瞻
  • (?=^\d+\.\s) - 正前瞻,紧靠当前位置的右侧,需要
    • ^ - 行首
    • \d+ - 1+ 位数
    • \. - 一个点
    • \s - 一个空格。