简体   繁体   English

如何在gsub(或regex)R中找到字符串中的目录

[英]How find only directory in string with gsub (or regex) R

Im trying to read a directory from a txt-file. 我试图从txt文件中读取目录。 In the txt file the syntax is like follows: 在txt文件中,语法如下:

dirScript;"C:/User/Folder_1/R/Script-Folder 1/"
"empty line"

The information that I want is to get var equal to a string with the directory like this: 我想要的信息是将var等于一个字符串,其目录如下:

var <- "C:/User/Folder_1/R/Script-Folder 1/"
setwd(var)

And my R code looks like this: 我的R代码看起来像这样:

tempString           <- str_c(str_extract_all(textInTxtFile, regex("(?<=;).*"), simplify=TRUE), sep="", collapse="") 

# Real variable, gsub expression deletes extra backslashes added by functions above
var                  <- gsub("[^A-Za-z0-9/.:-_; ]", "", tempString)
# Tempstring: "\""C:/User/Folder 1/R/Script-Folder 1/"\""
# Var: "C:/User/Folder 1/R/ScriptFolder 1/"

# *UPDATE* Or like this, seems to work, Safe enough?
var                  <- gsub('"', "", tempString)

So my "-" disappears and also the "_". 所以我的“ - ”消失了,也就是“_”。 I don't understand why, I think that my regex should search anything after a semicolon ";", is this wrong? 我不明白为什么,我认为我的正则表达式应该在分号之后搜索任何内容“;”,这是错的吗? Maybe I shouldn't use str_c ? 也许我不应该使用str_c? (but does it make a difference?) (但它有所作为吗?)

Also how do i fix my gsub to not take away "-" and (?) "_"? 另外我如何修复我的gsub不带走“ - ”和(?)“_”? Or how fix my regex so that gsub isn't necessary, my regex or other functions there add some backslashes and citation-signs. 或者如何修复我的正则表达式以便gsub不是必需的,我的正则表达式或其他函数添加一些反斜杠和引号。

And lastly is there a way to check my directory after I've found my correct directory? 最后有没有办法在找到正确的目录后检查我的目录?

Is this a good idea to use? 这是一个好主意吗?

dir.create(file.path(mainDir, subDir), showWarnings = FALSE)
setwd(file.path(mainDir, subDir))

from: Check existence of directory and create if doesn't exist from: 检查目录是否存在,如果不存在则创建

Extra question: Is there a way to easy find if the direction is written with backslashes (windows standard), if so I would just use gsub("[\\]", "/", text). 额外的问题:如果方向是用反斜杠(windows标准)写的,有没有办法轻松找到,如果是这样我只会使用gsub(“[\\]”,“/”,text)。 I was thinking of just searching for a number of backslashes and if its bigger than say 3 I use this, but it's not too safe. 我正在考虑只搜索一些反斜杠,如果它比3更大,我使用它,但它不太安全。 Aslo it will complain in the regex I think since R will interpret backslash as escape character. Aslo它会在正则表达式中抱怨我认为因为R会将反斜杠解释为转义字符。

So the "extra question" is, how find and fix this to a good path: 所以“额外的问题”是,如何找到并解决这个问题:

dirScript;"C:\User\Folder_1\R\Script-Folder 1\"
"empty line"

I fixed it. 我修好了它。 Thanks to the helped I received. 感谢我收到的帮助。

I think my main problem was in the gsub-function. 我认为我的主要问题在于gsub-function。 It works with this now: 它现在适用于此:

var                  <- gsub('"', "", tempString)

Also my regex-expression with the str_c and all might add some weird stuff that give me an extra-string, but thats deleted by the new gsub. 另外我的str_c和所有的regex表达式可能会添加一些奇怪的东西,它们给了我一个额外的字符串,但是被新的gsub删除了。

Also, I tried it with a path with backslashes "\\" and it works. 另外,我尝试使用反斜杠“\\”的路径,它的工作原理。

I don't know if leave this question up, maybe not? 我不知道是否提出这个问题,也许不是吗? Comment what you think. 评论你的想法。

Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM