2

I wanna detect exact domain url in string and then change that with another string and finally make it clickable in TextView.

What I want:

this is sample text with one type of url mydomain.com/pin/123456. another type of url is mydomain.com/username.

Wel, I wrote this regex:

([Hh][tT][tT][pP][sS]?://)?(?:www\\.)?example\\.com/?.*

([Hh][tT][tT][pP][sS]?://)?(?:www\\.)?example\\.com/pin/?.*

this regex can detect:

http://www.example.com
https://www.example.com
www.example.com
example.com
Hhtp://www.example.com // and all other wrong type in http

with anything after .com

Issues:

1. How detect end of domain ( with space or dot)

2. How detect two type of domain, one with /pin/ and another without?

3. How to replace detected domain like mydomain.com/pin/123 with PostLink and mydomain.com/username with ProfileLink

4. I know how to make them clickable with Linkify but if it possible show me best way to provide content provider for links to open each link with proper activity

MAY3AM
  • 1,182
  • 3
  • 17
  • 42

2 Answers2

1

You could try:

([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?

which is a regex I found after a quick search here on stackoverflow:
Regular expression to find URLs within a string

I just removed the http:// part of that regex to fit your needs.

Be aware though that because of that it now tracks everything that is connected with a dot and no whitespace. For example: a.a would also be found

Community
  • 1
  • 1
Gildraths
  • 376
  • 1
  • 10
  • Dude, first i want to detect `specific domain url` not `all url` in string. second after i've found that i wanna to replace them with another string as I said in my question description and finally make clickable it by `Linkify`. thanks for your interest ;) – MAY3AM Jun 20 '16 at 11:31
  • @MAY3AM `demo.com+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?` will give you all the strings matching to the domain demo.com . If pin is always at the same position: `demo.com\/pin+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?` I would then compare both results, since regex is not good at excluding results – Gildraths Jun 20 '16 at 12:20
1

With special thanks of Gildraths

Answer to question 1

String urlRegex = "(https?://)?(?:www\\.)?exampl.com+([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?";
Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(textString);

Answer to question 2, 3

while(matcher.find()){

    // Answer to question 2 - If was true, url contain "/pin"
    boolean contain = matcher.group().indexOf("/pin/") >= 0;

    if(contain){

        String profileId = matcher.group().substring(matcher.group().indexOf("/pin/") + 5, matcher.group().length());

    }

    // Answer to question 3 - replace match group with custom text
    textString = textString.replace(matcher.group(), "@" + profileId);
}

Answer to question 4

// Pattern to detect replaced custom text
Pattern profileLink     = Pattern.compile("[@]+[A-Za-z0-9-_]+\\b");

// Schema
String Link             = "content://"+Context.getString(R.string.profile_authority)+"/";

// Make it linkify ;)
Linkify.addLinks(textView, profileLink, Link);
Community
  • 1
  • 1
MAY3AM
  • 1,182
  • 3
  • 17
  • 42