Shubham AgaRwal
Shubham AgaRwal

Reputation: 4823

Android pathPattern Regex issue

I am facing a problem related to Android Intent-Filters esp. in data-element with a path pattern Reference: https://developer.android.com/guide/topics/manifest/data-element

I need to match a pattern for valid URLs like

anything-to-anything-trains

fromString-to-toString-trains

Below mentioned pattern is working almost perfectly

<data android:pathPattern=".*to.*trains" />
<data android:pathPattern=".*to.*trains/" />

However, this pattern is failing when from or to contain keyword 't'

e.g.

  1. t-to-a-trains
  2. a-to-t-trains
  3. a-to-ata-trains
  4. ata-to-aa-trains

For the given problem, anything including the keyword 't' should be matched with using the path pattern. Can you please suggest to me how to write a proper pattern for such a situation? I am a beginner in creating path patterns (regex or wildcard or pattern glob) but the pattern that I used in android:pathPattern work in regex testing sites but not with path pattern.

Additionally, Can we support regex with a special symbol like hyphen '-'?

I see symbol # doesn't work due to the Pattern matcher requirement.


According to PATTERN_SIMPLE_GLOB, the path pattern doesn't work how it is documented as there is no need to escape the Asterix.

Doc:

Because '' is used as an escape character when the string is read from XML (before it is parsed as a pattern), you will need to double-escape: For example, a literal '' would be written as "\" and a literal '' would be written as "\\". This is basically the same as what you would need to write if constructing the string in Java code.


Note: This question is not related to working of regex or not. Pattern/Regex working fine on regex tester site doesn't mean it will work on Android Platform as Andriod Intent filters have some limitation in parsing. It has only two wildcards as per the document.


To test the deeplink, please use following adb command:

adb shell am start -a android.intent.action.VIEW -d "deep link" [package name (optional)]

Example:

 adb shell am start -a android.intent.action.VIEW -d "https://www.website.com/a-to-a-trains" com.website.sample.package

Upvotes: 1

Views: 3224

Answers (2)

Darkman
Darkman

Reputation: 2981

Looking at the PATTERN_SIMPLE_GLOB doc, not much you can do.

In this syntax, you can use the '*' character to match against zero or more occurrences of the character immediately before. If the character before it is '.' it will match any character. The character '' can be used as an escape. This essentially provides only the '*' wildcard part of a normal regexp

So this should work.

.*-to-.*-trains

Upvotes: 1

Aman Gupta -  ΔMΔN
Aman Gupta - ΔMΔN

Reputation: 3007

You can use the special characters using \ (backslash).

i.e.

\- or \.

And for your solution, this may work,

[A-Za-z0-9].*\-.[A-Za-z0-9].*

Upvotes: 0

Related Questions