为什么regex将文件名与扩展名分离在ColdFusion中不起作用? [英] Why would regex to separate filename from extension not work in ColdFusion?

查看:128
本文介绍了为什么regex将文件名与扩展名分离在ColdFusion中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ColdFusion中尝试检索没有扩展名的文件名。我使用以下函数:
REMatchNoCase((。+?)(\。[^。] * $ | $),Doe,John 8.15.2012.docx );



我想这样返回一个数组: [Doe,John 8.15.2012 docx]
,但我总是得到一个数组,一个元素 - 整个文件名: [Doe,John 8.15.2012.docx code>



我试过rexv.org上的regex字符串,它按预期工作,但不是在ColdFusion。我收到了这个SO问题的字符串:正则表达式问题:获取文件名没有扩展一次性?在



ColdFusion是否使用不同的语法?



感谢。

解决方案

你没有得到预期的结果...



得到一个单一数组与整个文件名的原因是因为你的模式匹配整个文件名,并匹配

捕获两个组,但 rematch 返回匹配数组,而不是捕获的组的数组,因此您不会看到这些组。

answer



如何解决问题...



如果你正在处理简单文件(即没有 .htaccess 或类似的),那么最简单的解决方案是只使用...

  ListLast (filename,'。')

....只获取文件扩展名,无扩展名您可以...

  rematch('。+(?= \。[^。] + $ )',filename)

这使用前瞻来确保有一个后跟至少一个非 - 在字符串的末尾,但(因为它是一个前瞻),它被排除在匹配您只能在匹配中获得预扩展部分。)



处理非扩展文件 .htaccess README ),您可以将上述正则表达式修改为。+ ?:\。[^。] +)?$)这基本上做同样的事情,除了使扩展可选。但是,没有一个简单的方法来更新ListLast方法为这些(猜测你需要检查 len(extension)LT len(filename)-1
$ b $bÚ



(可选)访问捕获的群组...



如果要获取实际捕获的组,在CF中执行此操作的最近本地方法是使用 refind 函数,第四个参数设置为true - 但是,这只会提供位置和长度 - 要求您使用 mid 自行解压缩。



因为这个原因, 改进了CF的正则表达式实现,称为cfRegex ,它允许您直接返回组文本(即不会中间乱码)。



如果您要使用cfRegex,可以使用原始模式,如下所示:

  RegexMatch('(。+?)(\。[^。] * $ | $)',filename,1,0,'groups')

或使用命名参数:

  RegexMatch pattern ='(。+?)(\。[^。] * $ | $)',text = filename,returntype ='groups')

并且返回一个匹配数组,每个元素都是匹配的捕获组的数组。



如果你正在处理捕获的群体的大量正则表达式工作, cfRegex 绝对比使用CF的re方法更好。 / p>

如果你关心的是扩展名和/或文件名扩展名被排除,那么上面的例子就足够了。


I'm trying to retrieve a filename without the extension in ColdFusion. I am using the following function: REMatchNoCase( "(.+?)(\.[^.]*$|$)" , "Doe, John 8.15.2012.docx" );

I would like this to return an array like: ["Doe, John 8.15.2012","docx"] but instead I always get an array with one element - the entire filename:["Doe, John 8.15.2012.docx"]

I tried the regex string above on rexv.org and it works as expected, but not on ColdFusion. I got the string from this SO question: Regex Question: Get Filename Without Extension in One Shot?In

Does ColdFusion use a different syntax? Or am I doing something wrong?

Thanks.

解决方案

Why you're not getting expected results...

The reason you are getting a one-item array with the whole filename is because your pattern matches the entire filename, and matches once.

It is capturing the two groups, but rematch returns arrays of matches, not arrays of the captured groups, so you don't see those groups.

How to solve the problem...

If you are dealing with simple files (i.e. no .htaccess or similar), then the simplest solution is to just use...

ListLast( filename , '.' )

....to get only the file extension and to get the name without extension you can do...

rematch( '.+(?=\.[^.]+$)' , filename )

This uses a lookahead to ensure there is a . followed by at least one non-. at the end of the string, but (since it's a lookahead) it is excluded from the match (so you only get the pre-extension part in your match).

To deal with non-extensioned files (e.g. .htaccess or README) you can modify the above regex to .+(?=(?:\.[^.]+)?$) which basically does the same thing except making the extension optional. However, there isn't a trivial way to get update the ListLast method for these (guess you'd need to check len(extension) LT len(filename)-1 or similar).

(optional) Accessing captured groups...

If you want to get at the actual captured groups, the closest native way to do this in CF is using the refind function, with the fourth argument set to true - however, this only gives you positions and lengths - requiring that you use mid to extract them yourself.

For this reason (amongst many others), I've created an improved regex implementation for CF, called cfRegex, which lets you return the group text directly (i.e. no messing around with mid).

If you wanted to use cfRegex, you can do so with your original pattern like so:

RegexMatch( '(.+?)(\.[^.]*$|$)' , filename , 1 , 0 , 'groups' )

Or with named arguments:

RegexMatch( pattern='(.+?)(\.[^.]*$|$)' , text=filename , returntype='groups' )

And you get returned an array of matches, within each element being an array of the captured groups for that match.

If you're doing lots of regex work dealing with captured groups, cfRegex is definitely better than doing it with CF's re methods.

If all you care about is getting the extension and/or the filename with extension excluded then the previous examples above are sufficient.

这篇关于为什么regex将文件名与扩展名分离在ColdFusion中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆