Android Q的ModernMediaScanner.java文件中以下两个正则表达式,具体该怎么解读?

    private static final Pattern PATTERN_VISIBLE = Pattern.compile(            "(?i)^/storage/[^/]+(?:/[0-9]+)?(?:/Android/sandbox/([^/]+))?$");    private static final Pattern PATTERN_INVISIBLE = Pattern.compile(            "(?i)^/storage/[^/]+(?:/[0-9]+)?(?:/Android/sandbox/([^/]+))?/Android/(?:data|obb)$");

代码具体参考:https://source.codeaurora.org/quic/la/platform/packages/providers/MediaProvider/tree/src/com/android/providers/media/scan/ModernMediaScanner.java?h=aosp-new/master#138

先看下正则表达式的语法:

^

匹配输入字符串开始的位置。如果设置了 RegExp 对象的 Multiline 属性,^ 还会与"\n"或"\r"之后的位置匹配。

$

匹配输入字符串结尾的位置。如果设置了 RegExp 对象的 Multiline 属性,$ 还会与"\n"或"\r"之前的位置匹配。

*

零次或多次匹配前面的字符或子表达式。例如,zo* 匹配"z"和"zoo"。* 等效于 {0,}。

+

一次或多次匹配前面的字符或子表达式。例如,"zo+"与"zo"和"zoo"匹配,但与"z"不匹配。+ 等效于 {1,}。

?

零次或一次匹配前面的字符或子表达式。例如,"do(es)?"匹配"do"或"does"中的"do"。? 等效于 {0,1}。

(?:pattern)

匹配 pattern 但不捕获该匹配的子表达式,即它是一个非捕获匹配,不存储供以后使用的匹配。这对于用"or"字符 (|) 组合模式部件的情况很有用。例如,'industr(?:y|ies) 是比 'industry|industries' 更经济的表达式。

x|y

匹配 x 或 y。例如,'z|food' 匹配"z"或"food"。'(z|f)ood' 匹配"zood"或"food"。

[xyz]

字符集。匹配包含的任一字符。例如,"[abc]"匹配"plain"中的"a"。

[^xyz]

反向字符集。匹配未包含的任何字符。例如,"[^abc]"匹配"plain"中"p","l","i","n"。

[a-z]

字符范围。匹配指定范围内的任何字符。例如,"[a-z]"匹配"a"到"z"范围内的任何小写字母。

参考:Java 正则表达式

疑问:
最前面的(?i)表示什么呢?

解答:

(?i) 表示所在位置右侧的表达式开启忽略大小写模式

参考:

Q:经常看见的正则前面的 (?i) (?s) (?m) (?is) (?im) 是什么意思?
A: 称为内联匹配模式,通常用内联匹配模式代替使用枚举值RegexOptions指定的全局匹配模式,写起来更简洁。
  (?i) 表示所在位置右侧的表达式开启忽略大小写模式
  (?s) 表示所在位置右侧的表达式开启单行模式。
  更改句点字符 (.) 的含义,以使它与每个字符(而不是除 \n 之外的所有字符)匹配。
  注意:(?s)通常在匹配有换行的文本时使用
  (?m) 表示所在位置右侧的表示式开启指定多行模式。
  更改 ^ 和 $ 的含义,以使它们分别与任何行的开头和结尾匹配,
  而不只是与整个字符串的开头和结尾匹配。
  注意:(?m)只有在正则表达式中涉及到多行的“^”和“$”的匹配时,才使用Multiline模式。
  上面的匹配模式可以组合使用,比如(?is),(?im)。
  另外,还可以用(?i:exp)或者(?i)exp(?-i)来指定匹配的有效范围。

原文链接:https://blog.csdn.net/zhang__ao/article/details/78092294

对后面的表达式的理解:

理解:
对于PATTERN_VISIBLE 可以理解为,以"/storage/"开头;
"[^/]+" 表示后面一个或多个除'/'外的字符;
"(?:/[0-9]+)?" 表示再跟零个或多个,至少一个0-9的数字;那是至少一个数字,还是可以一个数字都没有呢?
"(?:/Android/sandbox/([^/]+))?" 理解同上,是有零个或一个字符串"/Android/sandbox/",并且字符串后面不是'/'
'$' 表示字符串结束;
 

具体通过一个java示例来确定PATTERN_VISIBLE

        String[] paths = {"/storage/emulated/0",                "/storage/sdcard1",                "/storage/usbotg",                "/storage/5D33-1608",                "/storage/0",                "/storage/a",                "/storage/2emulated/0",                "/storage",                "/storage//emulated/0",                "/storage///emulated/0",                "/storage/emulated/0/",                "/storage/5D33-1608/",                "/system/media/audio",                "/storage/emulated/0/Android/sandbox",                "/storage/emulated/0/Android/sandbox/",                "/storage/emulated/0/Android/sandbox/abc",                "/storage/emulated/0/Android/sandbox/abc/",                "/storage/emulated/0/Android/sandbox/abc/def"        };        for (String path : paths) {            if (PATTERN_VISIBLE.matcher(path).matches()) {                System.out.println("PATTERN_VISIBLE matches " + path);            } else {                System.out.println("PATTERN_VISIBLE NOT matches " + path);            }        }

输出结果如下:

PATTERN_VISIBLE matches /storage/emulated/0
PATTERN_VISIBLE matches /storage/sdcard1
PATTERN_VISIBLE matches /storage/usbotg
PATTERN_VISIBLE matches /storage/5D33-1608
PATTERN_VISIBLE matches /storage/0
PATTERN_VISIBLE matches /storage/a
PATTERN_VISIBLE matches /storage/2emulated/0

PATTERN_VISIBLE NOT matches /storage
PATTERN_VISIBLE NOT matches /storage//emulated/0
PATTERN_VISIBLE NOT matches /storage///emulated/0
PATTERN_VISIBLE NOT matches /storage/emulated/0/
PATTERN_VISIBLE NOT matches /storage/5D33-1608/
PATTERN_VISIBLE NOT matches /system/media/audio
PATTERN_VISIBLE NOT matches /storage/emulated/0/Android/sandbox
PATTERN_VISIBLE NOT matches /storage/emulated/0/Android/sandbox/
PATTERN_VISIBLE matches /storage/emulated/0/Android/sandbox/abc
PATTERN_VISIBLE NOT matches /storage/emulated/0/Android/sandbox/abc/
PATTERN_VISIBLE NOT matches /storage/emulated/0/Android/sandbox/abc/def

更多相关文章

  1. android 输入框自动匹配-AutoCompleteTextView
  2. android TextUtils的使用
  3. TableLayout 中 stretchColumns的用法
  4. Android系统移植(三)-按键字符表
  5. Android中Input型输入设备驱动原理分析(一)
  6. Android关于No Activity found to handle Intent的问题
  7. Android原生json类org.json.JSONObject初步使用
  8. android之调用webservice 实现图片上传
  9. android之调用webservice 实现图片上传

随机推荐

  1. Android签到功能的实现
  2. android MediaPlayer 错误代码(error cod
  3. Android(安卓)在Camera 的 SurfaceView添
  4. android 晃动
  5. Android(安卓)dialog设置无背景
  6. Android(安卓)采用DOM解析XML内容 【学习
  7. jWebSocket for Android
  8. android 圆角布局
  9. android sms function send function
  10. Android(安卓)PopupWindow显示位置和显示