如何使用正则表达式提取字符串中的文本?

How to extract the text in the string using regular expression?

我从网站上抓取了以下文本:

\n\tsfw=1;\n\tcaptions=[[\"2017-05\",2850],[\"2017-06\",3450],[\"2017-07\",3350] ,[\"2017-08\",3650],[\"2017-09\",4250],[\"2017-10\",4600],[\"2017-11\",4750],[ \"2017-12\",5500],[\"2018-01\",7300],[\"2018-02\",7700],[\"2018-03\",11350],[\" 2018-04\",10900],[\"2018-05\",11500],[\"2018-06\",10800],[\"2018-07\",13200],[\"2018- 08\",14200],[\"2018-09\",15900],[\"2018-10\",19700],[\"2018-11\",21800],[\"2018-12\ ",18300],[\"2019-01\",18550],[\"2019-02\",18150],[\"2019-03\",18050],[\"2019-04\", 18850],[\"2019-05\",71000],[\"2019-06\",83200],[\"2019-07\",72650],[\"2019-08\",80400] ,[\"2019-09\",100600],[\"2019-10\",114000],[\"2019-11\",110250],[\"2019-12\",107100],[ \"2020-01\",116050],[\"2020-02\",117950],[\"2020-03\",145350]];\n

我想提取数字“2850”、“3450”等文本。你能告诉我如何为此编写正则表达式吗?谢谢

这里是抓取r中文本的代码:

webpage <- read_html("https://imgflip.com/meme/Drake-Hotline-Bling")
text <- html_nodes(webpage,xpath = '//script') %>% html_text()
text <- text[8]

您可以使用 stringr 包中的 str_match_all。这将查找您选择的两个分隔符之间的所有内容(在本例中我选择了 \",])。

这个函数的通用形式是:stringr::str_match_all(vector, "delimiter(.*?)delimiter")(注意使用stringr::str_match只匹配第一个实例)

vec <- '\n\tsfw=1;\n\tcaptions=[[\"2017-05\",2850],[\"2017-06\",3450],[\"2017-07\",3350],[\"2017-08\",3650],[\"2017-09\",4250],[\"2017-10\",4600],[\"2017-11\",4750],[\"2017-12\",5500],[\"2018-01\",7300],[\"2018-02\",7700],[\"2018-03\",11350],[\"2018-04\",10900],[\"2018-05\",11500],[\"2018-06\",10800],[\"2018-07\",13200],[\"2018-08\",14200],[\"2018-09\",15900],[\"2018-10\",19700],[\"2018-11\",21800],[\"2018-12\",18300],[\"2019-01\",18550],[\"2019-02\",18150],[\"2019-03\",18050],[\"2019-04\",18850],[\"2019-05\",71000],[\"2019-06\",83200],[\"2019-07\",72650],[\"2019-08\",80400],[\"2019-09\",100600],[\"2019-10\",114000],[\"2019-11\",110250],[\"2019-12\",107100],[\"2020-01\",116050],[\"2020-02\",117950],[\"2020-03\",145350]];\n'

stringr::str_match_all(vec, "\",(.*?)]")[[1]][, 2]
[1]  "2850"   "3450"   "3350"   "3650"   "4250"   "4600"   "4750"   "5500"   "7300"   "7700"   "11350"  "10900"  "11500"  "10800"  "13200"  "14200"  "15900" 
[18] "19700"  "21800"  "18300"  "18550"  "18150"  "18050"  "18850"  "71000"  "83200"  "72650"  "80400"  "100600" "114000" "110250" "107100" "116050" "117950"
[35] "145350"