提取由未命名 (pattern)
和命名 (?<name>pattern)
捕获组定义的任意数量的匹配项。
如果您需要覆盖默认操作优先级但不想捕获结果,请使用非捕获组 (?:pattern)
。
参数
- string
-
输入向量。或者是一个字符向量,或者是可强制转换为一个的东西。
- pattern
-
与其他 stringr 函数不同,
str_match()
仅支持正则表达式,如vignette("regular-expressions")
中所述。该模式应至少包含一个捕获组。
值
-
str_match()
:行数与string
/pattern
长度相同的字符矩阵。第一列是完整匹配,后面是每个捕获组的一列。如果您使用“命名捕获组”,即(?<name>pattern')
,这些列将被命名。 -
str_match_all()
:与string
/pattern
长度相同的列表,包含字符矩阵。每个矩阵都有如上所述的列和每个匹配的一行。
也可以看看
str_extract()
用于提取完整匹配,stringi::stri_match()
用于底层实现。
例子
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 and 842 566 4692", "Work: 579-499-7527", "$1000",
"Home: 543.355.3679")
phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})"
str_extract(strings, phone)
#> [1] "219 733 8965" "329-293-8753" NA "595 794 7569"
#> [5] "387 287 6718" NA "233.398.9187" "482 952 3315"
#> [9] "239 923 8115" "579-499-7527" NA "543.355.3679"
str_match(strings, phone)
#> [,1] [,2] [,3] [,4]
#> [1,] "219 733 8965" "219" "733" "8965"
#> [2,] "329-293-8753" "329" "293" "8753"
#> [3,] NA NA NA NA
#> [4,] "595 794 7569" "595" "794" "7569"
#> [5,] "387 287 6718" "387" "287" "6718"
#> [6,] NA NA NA NA
#> [7,] "233.398.9187" "233" "398" "9187"
#> [8,] "482 952 3315" "482" "952" "3315"
#> [9,] "239 923 8115" "239" "923" "8115"
#> [10,] "579-499-7527" "579" "499" "7527"
#> [11,] NA NA NA NA
#> [12,] "543.355.3679" "543" "355" "3679"
# Extract/match all
str_extract_all(strings, phone)
#> [[1]]
#> [1] "219 733 8965"
#>
#> [[2]]
#> [1] "329-293-8753"
#>
#> [[3]]
#> character(0)
#>
#> [[4]]
#> [1] "595 794 7569"
#>
#> [[5]]
#> [1] "387 287 6718"
#>
#> [[6]]
#> character(0)
#>
#> [[7]]
#> [1] "233.398.9187"
#>
#> [[8]]
#> [1] "482 952 3315"
#>
#> [[9]]
#> [1] "239 923 8115" "842 566 4692"
#>
#> [[10]]
#> [1] "579-499-7527"
#>
#> [[11]]
#> character(0)
#>
#> [[12]]
#> [1] "543.355.3679"
#>
str_match_all(strings, phone)
#> [[1]]
#> [,1] [,2] [,3] [,4]
#> [1,] "219 733 8965" "219" "733" "8965"
#>
#> [[2]]
#> [,1] [,2] [,3] [,4]
#> [1,] "329-293-8753" "329" "293" "8753"
#>
#> [[3]]
#> [,1] [,2] [,3] [,4]
#>
#> [[4]]
#> [,1] [,2] [,3] [,4]
#> [1,] "595 794 7569" "595" "794" "7569"
#>
#> [[5]]
#> [,1] [,2] [,3] [,4]
#> [1,] "387 287 6718" "387" "287" "6718"
#>
#> [[6]]
#> [,1] [,2] [,3] [,4]
#>
#> [[7]]
#> [,1] [,2] [,3] [,4]
#> [1,] "233.398.9187" "233" "398" "9187"
#>
#> [[8]]
#> [,1] [,2] [,3] [,4]
#> [1,] "482 952 3315" "482" "952" "3315"
#>
#> [[9]]
#> [,1] [,2] [,3] [,4]
#> [1,] "239 923 8115" "239" "923" "8115"
#> [2,] "842 566 4692" "842" "566" "4692"
#>
#> [[10]]
#> [,1] [,2] [,3] [,4]
#> [1,] "579-499-7527" "579" "499" "7527"
#>
#> [[11]]
#> [,1] [,2] [,3] [,4]
#>
#> [[12]]
#> [,1] [,2] [,3] [,4]
#> [1,] "543.355.3679" "543" "355" "3679"
#>
# You can also name the groups to make further manipulation easier
phone <- "(?<area>[2-9][0-9]{2})[- .](?<phone>[0-9]{3}[- .][0-9]{4})"
str_match(strings, phone)
#> area phone
#> [1,] "219 733 8965" "219" "733 8965"
#> [2,] "329-293-8753" "329" "293-8753"
#> [3,] NA NA NA
#> [4,] "595 794 7569" "595" "794 7569"
#> [5,] "387 287 6718" "387" "287 6718"
#> [6,] NA NA NA
#> [7,] "233.398.9187" "233" "398.9187"
#> [8,] "482 952 3315" "482" "952 3315"
#> [9,] "239 923 8115" "239" "923 8115"
#> [10,] "579-499-7527" "579" "499-7527"
#> [11,] NA NA NA
#> [12,] "543.355.3679" "543" "355.3679"
x <- c("<a> <b>", "<a> <>", "<a>", "", NA)
str_match(x, "<(.*?)> <(.*?)>")
#> [,1] [,2] [,3]
#> [1,] "<a> <b>" "a" "b"
#> [2,] "<a> <>" "a" ""
#> [3,] NA NA NA
#> [4,] NA NA NA
#> [5,] NA NA NA
str_match_all(x, "<(.*?)>")
#> [[1]]
#> [,1] [,2]
#> [1,] "<a>" "a"
#> [2,] "<b>" "b"
#>
#> [[2]]
#> [,1] [,2]
#> [1,] "<a>" "a"
#> [2,] "<>" ""
#>
#> [[3]]
#> [,1] [,2]
#> [1,] "<a>" "a"
#>
#> [[4]]
#> [,1] [,2]
#>
#> [[5]]
#> [,1] [,2]
#> [1,] NA NA
#>
str_extract(x, "<.*?>")
#> [1] "<a>" "<a>" "<a>" NA NA
str_extract_all(x, "<.*?>")
#> [[1]]
#> [1] "<a>" "<b>"
#>
#> [[2]]
#> [1] "<a>" "<>"
#>
#> [[3]]
#> [1] "<a>"
#>
#> [[4]]
#> character(0)
#>
#> [[5]]
#> [1] NA
#>
相关用法
- R stringr str_which 查找匹配索引
- R stringr str_extract 提取完整的匹配项
- R stringr str_subset 查找匹配元素
- R stringr str_escape 转义正则表达式元字符
- R stringr str_trim 删除空格
- R stringr str_sub 使用子字符串的位置获取和设置子字符串
- R stringr str_replace_na 把NA变成“NA”
- R stringr str_trunc 将字符串截断至最大宽度
- R stringr str_like 以与 SQL 的 LIKE 运算符相同的方式检测模式
- R stringr str_length 计算长度/宽度
- R stringr str_detect 检测是否存在匹配
- R stringr str_count 计算匹配次数
- R stringr str_split 将字符串分成几段
- R stringr str_unique 删除重复的字符串
- R stringr str_remove 删除匹配的模式
- R stringr str_pad 将字符串填充到最小宽度
- R stringr str_equal 判断两个字符串是否相等
- R stringr str_view 查看字符串和匹配项
- R stringr str_glue 用胶水插补
- R stringr str_conv 指定字符串的编码
- R stringr str_order 对字符向量进行排序、排名或排序
- R stringr str_starts 检测开始/结束时是否存在匹配
- R stringr str_c 将多个字符串连接成一个字符串
- R stringr str_wrap 将单词包装成格式良好的段落
- R stringr str_dup 复制字符串
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Extract components (capturing groups) from a match。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。