pathPattern匹配的文件扩展名,如果一段时间在其他地方存在的文件名不工作?文件名、其他地方、文件扩展名、存在

2023-09-12 21:59:32 作者:我想有壹個屬于我們的未來

我看用pathPattern来定义一个意向过滤特定的文件扩展名/类型的例子不胜枚举;例如, pathPattern =* \\。XYZ

I see numerous examples of using pathPattern to define an intent-filter for a specific file extension/type; for example, pathPattern=".*\\.xyz".

不幸的是,这似乎并没有正常工作,如果有问题的文件有一个时期的路径在其他地方;例如,my.filename.xyz。

Unfortunately, this does not appear to work properly if the file in question has a period elsewhere in the path; for example "my.filename.xyz".

有没有pathPattern语法,将同时匹配myfilename.xyz和my.filename.xyz?

Is there a pathPattern syntax that will match both "myfilename.xyz" and "my.filename.xyz"?

推荐答案

Android团队选择了实施pathPattern一个不幸的方式。你可以看看如何模式匹配的android.os.PatternMatch类实现的:

The android team chose an unfortunate way to implement pathPattern. You can take a look at how the pattern matching is implemented in the android.os.PatternMatch class:

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/PatternMatcher.java

我们已经习惯了。*工作像它在一个普通的EX pression,其中*匹配是贪婪的,并会匹配尽可能多的字符可能。在PatterMatch的实施,与之匹配的是不贪。在*将匹配尽可能多的字符就可以了,直到它找到一个匹配的字符串中的一个字符。

We're used to .* working like it does in a regular expression, where the * match is greedy and will match as many characters as possible. In PatterMatch's implementation, the match is not greedy. The .* will match as many characters as it can, until it finds a match for the next character in the string.

例如:

字符串:/mnt/my.file.mytype pathPattern:*。\\ MYTYPE

String: "/mnt/my.file.mytype" pathPattern: ".*\\.mytype"

在*的pathPattern将匹配串到/ mnt /我的,因此将无法匹配字符串。

The ".*" in the pathPattern will match the substring "/mnt/my", and hence will fail to match the string.

由于这个限制,我没有看到一个方法来写一个pathPattern可以匹配在.mytype结尾的任意字符串。你能做的最好的就是按照Jason的解决方案,以增加额外的模式来匹配尽可能多的点路径,你愿意来指定模式。

Given this limitation, I don't see a way to write a pathPattern that can match any string that ends in ".mytype". The best you can do is follow Jason's solution to add additional patterns to match paths with as many dots as you are willing to specify patterns.

 
精彩推荐