我正在研究从文本中提取-使用r中的'stringr'
包。我发现了这个例子:
strings <- c(" 219 733 8965", "329-293-8753 ", "banana", "595 794 7569",
"387 287 6718", "apple", "233.398.9187 ", "482 952 3315",
"239 923 8115", "842 566 4692", "Work: 579-499-7527", "$1000",
"Home: 543.355.3679")
pattern <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})"
str_extract(strings, pattern)
str_extract_all(strings, pattern)
但是我的字符串是下面的格式:
strings <- c("87225324","65-62983211","65-6298-3211","8722 5324","(65) 6296-2995","(65) 6660 8060","(65) 64368308","+65 9022 7744","+65 6296-2995","+65-6427 8436","+65 6357 3323/322")
但我不确定pattern
提取所有上述格式。
下面的代码涵盖了您的问题中的情况。如果你在数据中找到其他字符组合,希望你可以推广它。
# Phone numbers (I've added an additional number with the "/" character)
strings <- c("87225324","65-62983211","65-6298-3211","8722 5324",
"(65) 6296-2995","(65) 6660 8060","(65) 64368308","+65 9022 7744",
"+65 6296-2995","+65-6427 8436","+65 6357 3323/322", "+65 4382 6922/6921")
# Remove all non-numeric characters except "/" (your string doesn't include any
# text like "Work:" or "Home:", but I included a regex to deal with those cases
# as well)
strings.cleaned = gsub("[- .)(+]|[a-zA-Z]*:?","", strings)
# If you're sure there are no other non-numeric characters you need to deal with
# separately, then you can also do the following instead of the code above:
# gsub("[^0-9/]","", strings). This regex matches any character that's not
# a digit or "/".
strings.cleaned
[1] "87225324" "6562983211" "6562983211" "87225324" "6562962995"
[6] "6566608060" "6564368308" "6590227744" "6562962995" "6564278436"
[11] "6563573323/322" "6543826922/6921"
# Separate string vector into the cleaned strings and the two "special cases" that we
# need to deal with separately
special.cases = strings.cleaned[grep("/", strings.cleaned)]
strings.cleaned = strings.cleaned[-grep("/", strings.cleaned)]
# Split each phone number with a "/" into two phone numbers
special.cases = unlist(lapply(strsplit(special.cases, "/"),
function(x) {
c(x[1],
paste0(substr(x[1], 1, nchar(x[1]) - nchar(x[2])), x[2]))
}))
special.cases
[1] "6563573323" "6563573322" "6543826922" "6543826921"
# Put the special.cases back with strings.cleaned
strings.cleaned = c(strings.cleaned, special.cases)
# Select last 8 digits from each phone number
phone.nums = as.numeric(substr(strings.cleaned, nchar(strings.cleaned) - 7,
nchar(strings.cleaned)))
phone.nums
[1] 87225324 62983211 62983211 87225324 62962995 66608060 64368308 90227744 62962995 64278436
[11] 63573323 63573322 43826922 43826921
pattern
参数接受任何正则表达式。因此,如果您使用str_extract_all(strings, pattern)
,例如,将正则表达式"[0-9]"
(提取字符串的任何数字部分)插入pattern
参数将返回一个列表,该列表仅包含来自strings
的元素的每个元素的数字。其他正则表达式的例子可以在这里找到:https://docs.python.org/2/library/re.html。
这是使用"[0-9]"
作为正则表达式从向量string
返回的内容:
str_extract_all(字符串,"[0 - 9]")
[[1]]
[1] "8" "7" "2" "2" "5" "3" "2" "4"
[[2]]
[1] "6" "5" "6" "2" "9" "8" "3" "2" "1" "1"
[[3]]
[1] "6" "5" "6" "2" "9" "8" "3" "2" "1" "1"
[[4]]
[1] "8" "7" "2" "2" "5" "3" "2" "4"
[[5]]
[1] "6" "5" "6" "2" "9" "6" "2" "9" "9" "5"
[[6]]
[1] "6" "5" "6" "6" "6" "0" "8" "0" "6" "0"
[[7]]
[1] "6" "5" "6" "4" "3" "6" "8" "3" "0" "8"
[[8]]
[1] "6" "5" "9" "0" "2" "2" "7" "7" "4" "4"
[[9]]
[1] "6" "5" "6" "2" "9" "6" "2" "9" "9" "5"
[[10]]
[1] "6" "5" "6" "4" "2" "7" "8" "4" "3" "6"
[[11]]
[1] "6" "5" "6" "3" "5" "7" "3" "3" "2" "3" "3" "2" "2"