【问题标题】:Regex in intent-filter of Android ManifestAndroid Manifest 的意图过滤器中的正则表达式
【发布时间】:2015-04-02 20:41:33
【问题描述】:

我正在尝试包含一个正则表达式,以实现对我的活动的深层链接。我们必须共享类似架构的活动,如下所示:

活动 A - http://www.google.com/a/process?abc=xyz 活动 B - http://www.google.com/a//?pqr=efg.

现在我在清单中声明深层链接如下:

<activity 
             android:name="com.xyz.samples.ActiivtyB" >
             <intent-filter android:label="SimpleB">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:host="www.google.com"
                    android:pathPrefix="/a/"
                    android:scheme="http" />
            </intent-filter>
        </activity>

上述活动 B 的声明解析了两个 url(如上所述),因为它们共享相同的路径,直到 '/a/'。 然而,这不是 Activity A 的问题,因为它的清单声明如下:

<activity 
                 android:name="com.xyz.samples.ActiivtyA" >
                 <intent-filter android:label="SimpleA">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="www.google.com"
                        android:pathPrefix="/a/process"
                        android:scheme="http" />
                </intent-filter>
            </activity>

因此我想到了使用“正则表达式”来识别 /a/ 类型的网址,我验证了正则表达式 here 并且它正在工作。即使我编写了一个简单的 Java 程序来验证这一点,它也能正常工作。正则表达式是 /a/)(?!\bsuccess\b).*

当我尝试使用上述正则表达式创建深层链接时,应用无法解析http://www.google.com/a//?pqr=efg 类型的 URL。清单更新如下:

<activity 
                 android:name="com.xyz.samples.ActiivtyB" >
                 <intent-filter android:label="SimpleB">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="www.google.com"
                        android:pathPattern="(/a/)(?!\\bsuccess\\b).\\*"
                        android:scheme="http" />
                </intent-filter>
            </activity>

任何有关如何在清单中声明正则表达式的帮助或见解都会非常有帮助。 我浏览了 Android 文档 here 并相应地更新了正则表达式。

【问题讨论】:

    标签: android regex android-manifest intentfilter deep-linking


    【解决方案1】:

    您错误地转义了您的模式 - .\\* 与文字字符串 .* 匹配。而且:

    pathPattern 不是真正的正则表达式,而是weird subset of regex。我认为没有办法使用这些选项排除 /a/success。

    【讨论】:

    • 迈克,看看有什么替代方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多