Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将正则表达式与kwic一起使用以获取所有匹配项?_R_Regex_Quanteda - Fatal编程技术网

R 如何将正则表达式与kwic一起使用以获取所有匹配项?

R 如何将正则表达式与kwic一起使用以获取所有匹配项?,r,regex,quanteda,R,Regex,Quanteda,我似乎无法使用quanteda的qwic获得所需的输出。以下是我尝试过的: library(quanteda) library(tidyverse) 鉴于本文 text <- "This is a phone number: 222-222-2222. Here's another phone number...(111)111 1111. This -- 333-3333 -- aint a complete phone number." 但kwic与任何东西都不

我似乎无法使用quanteda的
qwic
获得所需的输出。以下是我尝试过的:

library(quanteda)
library(tidyverse)
鉴于本文

text <- "This is a phone number: 222-222-2222. Here's another phone number...(111)111 1111. This -- 333-3333 -- aint a complete phone number."
但kwic与任何东西都不匹配。 这:

返回:

A tibble: 0 x 7
… with 7 variables: docname <chr>, from <int>, to <int>, pre <chr>, keyword <chr>,
  post <chr>, pattern <fct>
A tible:0 x 7
…有7个变量:docname、from、to、pre、keyword、,
柱子、图案
我的愿望是让它与所有电话号码匹配,在本例中是:

“222-222-2222。”

“(111)111 1111。”


(并将它们放在kwic输出的正常形式中,显示pre、post等)。

我已经尝试通过正则表达式生成
短语来匹配电话号码

库(quanteda)
图书馆(tidyverse)
文本%
打印(可打印)
#输出:
#[text1,6:7]号码:| 541 145-8884 |还有222-222-2222这是
#[text1,9:9]541 145-8884还有| 222-222-2222 |这里是(444
#[text1,11:14]还有222-222-2222这是|(444)111-1111 |号。
#[text1,18:20],编号| 555 666 7774 |

我试图通过正则表达式生成
短语来匹配电话号码

库(quanteda)
图书馆(tidyverse)
文本%
打印(可打印)
#输出:
#[text1,6:7]号码:| 541 145-8884 |还有222-222-2222这是
#[text1,9:9]541 145-8884还有| 222-222-2222 |这里是(444
#[text1,11:14]还有222-222-2222这是|(444)111-1111 |号。
#[text1,18:20],编号| 555 666 7774 |

不确定kwic的工作方式,但您可以使用
str_extract_all
中的模式来提取满足该模式的所有单词。
stringr::str_extract_all(text,regex.phone1)[[1]]
不确定kwic的工作方式,但您可以使用
str_extract_all
中的模式来提取满足该模式的所有单词。
stringr::str_extract_all(text,regex.phone1)[[1]]
另外,我原来的正则表达式匹配了一个前面有一个非letter的数字,但你的不匹配。它应该匹配这个
,前面有一个字符-111-111-1111。后面有一个非-too
另外,它应该匹配这个:
1112223333
(但这可能不是我原来的规范!)我已经编辑了答案,现在它将匹配
111222333
。在你的第一条评论中,我不明白你想要匹配哪种类型的数字,你能给我一些例子吗?另外,我原来的正则表达式匹配了一个前面有non-letter的数字,但是你的不匹配。它应该匹配这个
,前面有一个字符-111-111-1111。一个d后跟一个非#too
,也应该与此匹配:
111222333
(但可能这不在我的原始规范中!)我已经编辑了答案,现在它将匹配
111222333
。在你的第一条评论中,我不明白你想要匹配哪种类型的数字,你能给我一些例子吗?
regmatches(text,regexpr(regex.phone1,text))

" 222-222-2222." 

 kwic(
  x = text,
  pattern = regex.phone1,
  window = 5,
  valuetype = "regex",
  case_insensitive = TRUE
) %>%
  as_tibble

A tibble: 0 x 7
… with 7 variables: docname <chr>, from <int>, to <int>, pre <chr>, keyword <chr>,
  post <chr>, pattern <fct>