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

6
  • so why aren't you using Linkify? Commented Jun 20, 2016 at 8:27
  • @pskink How? can you provide snippet of code Commented Jun 20, 2016 at 8:29
  • I know how to make them clickable with Linkify, if you know so what code do you need? Commented Jun 20, 2016 at 8:33
  • @pskink Issue is regex not linkify Commented Jun 20, 2016 at 8:37
  • do your urls always contain a http/https or is it always like your example? Also is it always 1 or two 2 slashes or could it also be more? Commented Jun 20, 2016 at 8:49

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 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
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.