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
RegexOne示例5(图像文件名regex)_Regex - Fatal编程技术网

RegexOne示例5(图像文件名regex)

RegexOne示例5(图像文件名regex),regex,Regex,我把regex放在这里是过滤图像文件名和文件类型的正确方法吗 ^(\w)+\.(jpg|png|gif)$ 此练习的链接如下: ? 练习是从文件列表中捕获名称和扩展名: task text capture skip text .bash_profile skip text workspace.doc capture text img0912.jpg img0912,

我把regex放在这里是过滤图像文件名和文件类型的正确方法吗

^(\w)+\.(jpg|png|gif)$
此练习的链接如下: ?

练习是从文件列表中捕获名称和扩展名:

task           text                  capture
skip text      .bash_profile        
skip text      workspace.doc        
capture text   img0912.jpg          img0912, jpg    
capture text   updated_img0912.png  updated_img0912, png    
skip text      documentation.html       
capture text   favicon.gif          favicon, gif    
skip text      img0912.jpg.tmp      
skip text      access.lock

你只有一个错误:

使用
(\w)+
仅捕获文件名的最后一个字符。这叫做“”,这不是你想要的。将量词放入组中以捕获完整名称

    (\w+)
一些注释

  • 起始锚点“^”不一定是必需的,这取决于您正在寻找什么。对于那个例子来说,这没有什么区别

  • \w
    适用于您的示例,但不会捕获所有有效的文件名,因为它只包含字母、数字和下划线


  • 与其问它是否正确,不如运行它,看看是否得到了正确的输出?你也应该在这里复制问题的相关部分,而不是仅仅发布一个链接。