简体   繁体   English

匹配特定文件路径正则表达式

[英]Match Specific File Path Regex

so far I have this regex $fileregex = /([az]:\\\\\\\\([^\\\\\\\\^\\\\.])*)|(\\/[^\\/.])/i; 到目前为止,我有这个正则表达式$fileregex = /([az]:\\\\\\\\([^\\\\\\\\^\\\\.])*)|(\\/[^\\/.])/i; but I am very confused on what to do next. 但是我对下一步该做什么感到非常困惑。

I want to match strings in this format 我想以这种格式匹配字符串

c:\\something\\else\\something
c:\\something\\else\\something.whatever
/etc/whatever/something/here
/etc/here.txt
/
c:\\

But I don't want to match, for example 但是例如,我不想匹配

c:\oneslash\text.txt
\etc\hi

I am really stuck on my regex especially on repeating the optional path, as one could just request the root.Can anyone help me out with the regex? 我真的对我的正则表达式特别感兴趣,特别是在重复可选路径时,因为可以只请求根目录。有人可以帮我解决正则表达式吗?

This one should work: 这应该工作:

preg_match_all('%[A-Za-z]:\\\\\\\\(.*?\\\\\\\\)*.*|/(.*?/)*.*%m', $input, $regs, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($regs[0]); $i++) {
    # Matched text = $regs[0][$i];
}

Result: 结果:

在此处输入图片说明

Description of the Regex: 正则表达式的说明:

                Match either the regular expression below (attempting the next alternative only if this one fails)
    [A-Za-z]        Match a single character present in the list below
                      A character in the range between “A” and “Z”
                      A character in the range between “a” and “z”
    :               Match the character “:” literally
    \\\\              Match the character “\” literally
    \\\\              Match the character “\” literally
    (               Match the regular expression below and capture its match into backreference number 1
      .               Match any single character that is not a line break character
         *?              Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
      \\\\              Match the character “\” literally
      \\\\              Match the character “\” literally
    )*              Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    .               Match any single character that is not a line break character
      *               Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
|               Or match regular expression number 2 below (the entire match attempt fails if this one fails to match)
   /               Match the character “/” literally
   (               Match the regular expression below and capture its match into backreference number 2
      .               Match any single character that is not a line break character
         *?              Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
      /               Match the character “/” literally
   )*              Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   .               Match any single character that is not a line break character
      *               Between zero and unlimited times, as many times as possible, giving back as needed (greedy)

Have you try this: 您是否尝试过以下方法:

/^([a-zA-Z]\:|\\\\[^\/\\:*?"<>|]+\\[^\/\\:*?"<>|]+)(\\[^\/\\:*?"<>|]+)+(\.[^\/\\:*?"<>|]+)$/

This regular expression will match any valid file path. 此正则表达式将匹配任何有效的文件路径。 It checks local drives and network path. 它检查本地驱动器和网络路径。 The file extension is required. 文件扩展名是必需的。

Further references : The regular expression library 进一步的参考: 正则表达式库

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

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