Monday, 25 January 2021

Andoid 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


from Andoid pathPattern Regex issue

No comments:

Post a Comment