[英]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]
您可以使用str_match_all
package 中的stringr
。 這將查找您選擇的兩個分隔符之間的所有內容(在本例中,我選擇了\",
和]
)。
這個 function 的通用形式是: 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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.