You may use a (?<=[a-z])(?=[0-9])
lookaround based regex with tidyr::separate
:
> tidyr::separate(df, A, into = c("name", "value"), "(?<=[a-z])(?=[0-9])")
name value
1 enc 0
2 enc 10
3 enc 25
4 enc 100
5 harab 0
6 harab 25
7 harab 100
8 requi 0
9 requi 25
10 requi 100
The (?<=[a-z])(?=[0-9])
pattern matches a location in the string right in between a lowercase ASCII letter ((?<=[a-z])
) and a digit ((?=[0-9])
). The (?<=...)
is a positive lookahead that requires the presence of some pattern immediately to the left of the current location, and (?=...)
is a positive lookahead that requires the presence of its pattern immediately to the right of the current location. Thus, the letters and digits are kept intact when splitting.
Alternatively, you may use extract
:
extract(df, A, into = c("name", "value"), "^([a-z]+)(\d+)$")
Output:
name value
1 enc 0
2 enc 10
3 enc 25
4 enc 100
5 harab 0
6 harab 25
7 harab 100
8 requi 0
9 requi 25
10 requi 100
The ^([a-z]+)(\d+)$
pattern matches:
^
- start of input
([a-z]+)
- Capturing group 1 (column name
): one or more lowercase ASCII letters
(\d+)
- Capturing group 2 (column value
): one or more digits
$
- end of string.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…