kcvizer
kcvizer

Reputation: 137

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

I am trying to split row into multiple rows using the tidy R package.

This is a single cell in my dataset

column 1
 1. a
 2. b
33. c

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

When I use the above code:

I get

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

Upvotes: 1

Views: 832

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626952

You may use

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

Regex details

  • (?m) - ^ now matches start of a line position
  • (?!\A) - a negative lookahead that fails the match when at the start of a string
  • (?=^\d+\.\s) - a positive lookahead that, immediately to the right of the current location, requires
    • ^ - start of a line
    • \d+ - 1+ digits
    • \. - a dot
    • \s - a whitespace.

Upvotes: 2

Related Questions