R_Language 板


LINE

關鍵在於你pattern = "(.*)(\\d{4,7}$)"這句 解讀你的pattern意思是 (任意字元出現0~n次)(尾部出現4-7個數字) 執行比對的時候4614834的前面3個數字(461)被當成任意字元了 所以永遠只會回傳最後4個數字 小弟學藝不精,對大大寫的gsub不熟(尤其是replacement="\\2"的部分) 查了下發現好像是capturing group[1],但不是很能確定如何擷取需要的部分 我只知道\\1是return比對到的字串(有錯請指正) \\2就不知道是甚麼了 如何改你的code再有請其他大大出手協助 ¯\_(ツ)_/¯ ##### 以下提供兩個做法: (fname為最原始的字串 c("agarslawrence_204628_20026409_DMC_Assignment2_4614834-1.docx","..."...)) 方法1: base function: 用regexec搭配regmatches來確定是否正確比對出所想要的字串 regmatches( fname, regexec("(\\d{4,7})(-1.pdf|-1.docx|.pdf|.docx|_FINAL.pdf|-2.docx|_Assign2_DMC-1.pdf)$", fname) ) 根據我的pattern: "(\\d{4,7})(-1.pdf|-1.docx|.pdf|.docx|_FINAL.pdf|-2.docx|_Assign2_DMC-1.pdf)$" 回傳的結果會是一個list,每個element裡面又各有三個東西,分別為 1.完整比對結果 2.第1個子模式(\\d{4,7})比對結果 3.第2個子模式(.pdf|.docx...)比對結果 (ps. 括號括起來的就是子模式) [[1]] [1] "4614834-1.docx" "4614834" [3] "-1.docx" [[2]] [1] "0953.docx" "0953" [3] ".docx" [[3]] [1] "6589.pdf" "6589" ".pdf" [[4]] [1] "8752.pdf" "8752" ".pdf" ... 可以發現第2個element是我們要的東西 取出結果: regmatches( fname, regexec("(\\d{4,7})(-1.pdf|-1.docx|.pdf|.docx|_FINAL.pdf|-2.docx|_Assign2_DMC-1.pdf)$", fname) ) %>% sapply(.,function(x){x[2]}) [1] "4614834" "0953" "6589" [4] "8752" "7392" "4833" [7] "4127" "6490" "7389" [10] "6083" "0313" "2309" [13] "8928" "6822" "6193" [16] "0718" "6098" "8800" [19] "0384" "5572" "8847" 方法2: stringr::str_match data.frame(have=fname) %>% mutate(want=str_match(fname,"(\\d{4,7})(-1.pdf|-1.docx|.pdf|.docx|_FINAL.pdf|-2.docx|_Assign2_DMC-1.pdf)$")[,2]) 基本上做的事情跟上面一樣,只是一行完成 have want 1 agarslawrence_204628_20026409_DMC_Assignment2_4614834-1.docx 4614834 2 andrewstimothy_297293_19936198_DMC Assignment 2 0953.docx 0953 3 ashtonisabel_294366_19979255_DMC_2021S2_A2_6589.pdf 6589 4 beaumontjenna_294376_20033339_DMC_Assignment2_8752.pdf 8752 5 claytonphilipandrew_125548_20002099_DMC_Assignment2_7392-1.pdf 7392 6 eddydani_297300_19967943_DMC_Assignment2_4833.docx 4833 7 edwardsfelicity_281039_19732901_DMC_Assignment2_4127.pdf 4127 8 gannonsean_294373_19967863_DMC_Assignment2_6490.pdf 6490 9 gawjames_295684_19988820_DMC_Assignment2_7389.docx 7389 10 godwinsam_294371_20034982_DMC_Assignment2_unikey_6083_FINAL.pdf 6083 11 guanqi_LATE_296467_20039218_ DMC_Assignment2_0313-1.pdf 0313 12 herbertrob_110692_19622280_DMC_Assignment2_2309.pdf 2309 13 hopkinsnathan_293664_20025212_DMC_Assignment2_8928.pdf 8928 14 huangannette_265602_20028154_DMC_Assignment 2_6822.pdf 6822 15 ilaovince_294377_20026222_DMC_Assignment2_6193.docx 6193 16 lammichael_293038_19961008_DMC_assignment2_0718.docx 0718 17 lamyanyin_294380_19991757_DMC_Assignment2_6098.docx 6098 18 leekenneth_281025_19958430_DMC_Assignment2_8800-2.docx 8800 19 luimatthew_296777_20032458_DMC_Assignment2_0384-1.pdf 0384 20 mcphailsandi_283784_20036309_5572_Assign2_DMC-1.pdf 5572 21 millerjake_278059_20036475_DMC_Assignment2_8847.docx 8847 資料附註: [1] https://www.regular-expressions.info/rlanguage.html ########## btw, 還是希望以後發問請提供「可以直接複製」的資料 不然就算到了你的hackmd頁面,上面的程式碼根本無法執行,我還是要自己一行一行複製資料.... 幫忙意願會大幅降低...QQ file.names <- list.files( path=dir.assignment.2 ,pattern=".*\\.pdf$|.*\\.docx$" ,full.names=FALSE) # length(file.names) 21 ↑↑↑上面這行別人沒辦法執行啊啊啊啊啊!!!↑↑↑ 最簡單的做法就是使用dput() ex: dput(file.names) 然後把顯示的結果一字不漏地全部複製貼過來即可。 《回答問題拿不到任何好處純粹是交流,發問還請體諒版友們,一起創造良好的交流環境吧~~》 小小版工下台一鞠躬 ※ 引述《luenchang (luen)》之銘言: : [問題敘述]: : 請教先進們, : 我有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), 來自: 111.71.212.70 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1633849207.A.54B.html ※ 編輯: locka (111.71.212.70 臺灣), 10/10/2021 15:04:02 編輯上色 ※ 編輯: locka (111.71.212.70 臺灣), 10/10/2021 15:19: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燈, 水草

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

TOP