Regular expressions – Split a number with a number in R

How to split a string containing numbers (unknown digits) into two strings-the number and the rest of the string. Please note that there may be other undesirable characters in the string The number affected. For example:

"abc665abc12" -> "abc665abc", "12"
"abc665abc 182" -> "abc665abc", "182"
"abc665abc0" -> "abc665abc", "0"

Thank you!

You can also use strsplit

< pre>> x = c(“abc665abc12”, “abc665abc 182”, “abc665abc0”)
> strsplit(x, “(?<=[A-Za-z])\\s*(?=\ \d+$)", perl = TRUE)
[[1]]
[1] “abc665abc” “12”

[[2]]
[ 1] “abc665abc” “182”

[[3]]
[1] “abc665abc” “0”

How to Split a string containing numbers (unknown digits) into two strings-the number and the rest of the string. Please note that there may be other numbers in the string that should not be affected. For example:

"abc665abc12" -> "abc665abc", "12"
"abc665abc 182" -> "abc665abc", "182"
"abc665abc0" -> "abc665abc", "0"

Thank you!

You can also use strsplit

> x = c("abc665abc12", " abc665abc 182", "abc665abc0")
> strsplit(x, "(?<=[A-Za-z])\\s*(?=\\d+$)", perl = TRUE)
[[1]]
[1] "abc665abc" "12"

[[2]]
[1] "abc665abc" "182"

[[3]]
[1] "abc665abc" "0"

Leave a Comment

Your email address will not be published.