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/cn.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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP