Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 仅匹配一个引用,而不是连续引用_R_Regex - Fatal编程技术网

R 仅匹配一个引用,而不是连续引用

R 仅匹配一个引用,而不是连续引用,r,regex,R,Regex,我有一个从list.files(…,full.names=T)返回的带有目录路径的文件名。我想按/将文件名拆分以查找目录结构。我无法仅识别单个出现的/,例如 strsplit("C://dir1/dir2/txt.R", "/") # [[1]] # [1] "C:" "" "dir1" "dir2" "txt.R" 当我希望输出为: [1] "C://" "dir1" "dir2" "txt.R" 我看到的似乎是一个正则表达式的答案,但是,当我尝试获得“文字”匹配

我有一个从
list.files(…,full.names=T)
返回的带有目录路径的文件名。我想按
/
将文件名拆分以查找目录结构。我无法仅识别单个出现的
/
,例如

strsplit("C://dir1/dir2/txt.R", "/")
# [[1]]
# [1] "C:"    ""      "dir1"  "dir2"  "txt.R"
当我希望输出为:

[1] "C://"  "dir1"  "dir2"  "txt.R"
我看到的似乎是一个正则表达式的答案,但是,当我尝试获得“文字”匹配时,我得到了一个错误:

> strsplit("C://dir1/dir2/txt.R", "\/")
Error: '\/' is an unrecognized escape in character string starting ""\/"
事实上,该示例中的正则表达式在
R
中不起作用:

> grepl('([\w\/]+)\/amp(\/\w+[-\/]\w+\/?)', '/name/amp/test-123')
Error: '\w' is an unrecognized escape in character string starting "'([\w"

一个选项是匹配多个出现的
/
跳过
,同时在单个
/
/
后成功的单词边界上进行拆分

strsplit("C://dir1/dir2/txt.R", "[/]{2,}(*SKIP)(*F)|\\b[/]|(?<=[/])\\b", perl = TRUE)[[1]]
#[1] "C://"  "dir1"  "dir2"  "txt.R"
strsplit(“C://dir1/dir2/txt.R”,“[/]{2,}(*SKIP)(*F)\\\b[/]|”(?尝试以下代码:

strsplit("C://dir1/dir2/txt.R", "(?<=//)|(?<!/)/(?!/)", perl=TRUE)
strsplit(“C://dir1/dir2/txt.R”,”(?KISS


strsplit(“C://dir1/dir2/txt.R”,“\\b/\\b|”(?一种非常简单的匹配方法是

x <- "C://dir1/dir2/txt.R"
regmatches(x, gregexpr("[^/]+(?://)?", x))
#  or with stringr
str_extract_all(x, "[^/]+(?://)?")
# [[1]]
# [1] "C://"  "dir1"  "dir2"  "txt.R"

查看和。

使用:哇进行拆分。1)我不知道事情会如此复杂。2)你怎么这么快就想出了这个正则表达式?@Alex谢谢。我按照你描述的逻辑跳过了那些不止一个
/
答案的比赛。这样就超过了我的头脑,我在本周排名9+1。@TimBiegeleisen谢谢,是的,很高兴知道你进入了前10名。继续保持下去,单词边界可能不会改变在这里很好,特别是如果在
/
之前或之后有非单词字符。我理解这不是当前的情况,仅供参考。是的,@WiktorStribiżew我同意。如果这是一个与R相关的问题,根据当前上下文回答就足够了。
x <- "C://dir1/dir2/txt.R"
regmatches(x, gregexpr("[^/]+(?://)?", x))
#  or with stringr
str_extract_all(x, "[^/]+(?://)?")
# [[1]]
# [1] "C://"  "dir1"  "dir2"  "txt.R"
regmatches(x, gregexpr("[^/]+(?:(?<=^[[:alpha:]]:)//)?", x, perl=TRUE))
# or
regmatches(x, gregexpr("^[[:alpha:]]://|[^/]+", x))