R_Language 板


LINE

[問題敘述]: 請教先進們, 我有21個檔案。我想從檔案名稱的尾巴拿出數字的部分,位數介於4到7位。 檔案是pdf及docx. 我已先把不要的字串去除,直到尾巴部分留下數字,這部分 命名為have. 21個檔案中只有第一個檔案的目標數字是7位(4614834),第2到21個檔案都是4位。目前我的regex 拿出的數字全是4位數,不知道為何 \\d{4,7} 只從第1個檔案拿出4834,而不是4614834 。我把拿出的數字命名為 want [程式範例]: 請參考以下網頁第一個部分 Extract numbers of varying length from the end of strings https://hackmd.io/@Chang/string-manipulation-in-R 或以下的程式: 拿出檔名 file.names <- list.files( path=dir.assignment.2 ,pattern=".*\\.pdf$|.*\\.docx$" ,full.names=FALSE) # length(file.names) 21 拿出檔名尾數字 file.names.1 <- data.frame(file.name=file.names ,stringsAsFactors = F) %>% # Remove unwanted strings dplyr::mutate(have= stringr::str_remove_all( string=file.names ,pattern = "_Assign2_DMC-1.pdf|-1.docx|-1.pdf|_FINAL.pdf|-2.docx|.docx|.pdf") ,want=gsub(x=have, pattern = "(.*)(\\d{4,7})$", replacement = "\\2", perl = TRUE)) %>% #Keep wanted columns dplyr::select(have, want) 結果 # have want # 1 agarslawrence_204628_20026409_DMC_Assignment2_4614834 4834 # 2 andrewstimothy_297293_19936198_DMC Assignment 2 0953 0953 # 3 ashtonisabel_294366_19979255_DMC_2021S2_A2_6589 6589 # 4 beaumontjenna_294376_20033339_DMC_Assignment2_8752 8752 # 5 claytonphilipandrew_125548_20002099_DMC_Assignment2_7392 7392 # 6 eddydani_297300_19967943_DMC_Assignment2_4833 4833 # 7 edwardsfelicity_281039_19732901_DMC_Assignment2_4127 4127 # 8 gannonsean_294373_19967863_DMC_Assignment2_6490 6490 # 9 gawjames_295684_19988820_DMC_Assignment2_7389 7389 # 10 godwinsam_294371_20034982_DMC_Assignment2_unikey_6083 6083 # 11 guanqi_LATE_296467_20039218_ DMC_Assignment2_0313 0313 # 12 herbertrob_110692_19622280_DMC_Assignment2_2309 2309 # 13 hopkinsnathan_293664_20025212_DMC_Assignment2_8928 8928 # 14 huangannette_265602_20028154_DMC_Assignment 2_6822 6822 # 15 ilaovince_294377_20026222_DMC_Assignment2_6193 6193 # 16 lammichael_293038_19961008_DMC_assignment2_0718 0718 # 17 lamyanyin_294380_19991757_DMC_Assignment2_6098 6098 # 18 leekenneth_281025_19958430_DMC_Assignment2_8800 8800 # 19 luimatthew_296777_20032458_DMC_Assignment2_0384 0384 # 20 mcphailsandi_283784_20036309_5572 5572 # 21 millerjake_278059_20036475_DMC_Assignment2_8847 8847 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.197.45.209 (澳大利亞)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1633831748.A.F2E.html
1F:→ andrew43: ".*" 太貪心。留意到[ _]是分隔的重點,整個pattern可以 10/10 16:42
2F:→ andrew43: 改成 "([^_ ]+)([_ ]+)(\\d+)$",其實就是按 [_ ] 切開 10/10 16:42
3F:→ andrew43: 或是 "([^\\d]+)(\\d+)$" 或許也容易理解。 10/10 16:50
4F:→ andrew43: 你要抓取的東西pattern會很多種都可用,先找一個好懂的 10/10 16:54
5F:→ andrew43: 對於 ".*" 這種包山包海的集合,細心一點看待它。 10/10 16:56







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP