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

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

TOP