英文:
How to extract part from string?
问题
I need to extract whatever is between \\\
and _proj
.
我需要提取在 \\\
和 _proj
之间的内容。
英文:
If I have this string:
bb=c("\\dat\\Bjkjgf_Yloiut_dsezr_proj_111_999.txt","\\dat\\Bjkjgf_Yloezr_proj_111_999.txt")
I need to extract whatever between \\ and _proj
i can do this, but will work for one and not the other:
substr(bb, 1, 15)
the expected output
Bjkjgf_Yloiut_dsezr,Bjkjgf_Yloezr
答案1
得分: 1
使用 gsub
gsub(".*\\\\|_proj.*", "", bb)
[1] "Bjkjgf_Yloiut_dsezr" "Bjkjgf_Yloezr"
英文:
Using gsub
gsub(".*\\\\|_proj.*", "", bb)
[1] "Bjkjgf_Yloiut_dsezr" "Bjkjgf_Yloezr"
答案2
得分: 0
使用regmatches
:
sapply(regmatches(bb, regexec("dat\\\\(.*)_proj", bb)), `[`, 2)
#[1] "Bjkjgf_Yloiut_dsezr" "Bjkjgf_Yloezr"
或者使用stringr::str_match
:
stringr::str_match(bb, "dat\\\\(.*)_proj")[, 2]
英文:
With regmatches
:
sapply(regmatches(bb, regexec("dat\\\\(.*)_proj", bb)), `[`, 2)
#[1] "Bjkjgf_Yloiut_dsezr" "Bjkjgf_Yloezr"
Or stringr::str_match
:
stringr::str_match(bb, "dat\\\\(.*)_proj")[, 2]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论