0

I'm trying to get specific value inside a TextView, namely Youtube link.

I want to extract only the link from for example a TextView which contains "Check this out! (Youtube link here)". "Check this out! " needs to be omitted and only obtain the Youtube link. The TextView is user inputted, so it varies.

Any suggestion?

Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43
  • You have to get the entire text of the text view, and then parse it. If it always starts with "Check this out!" that's easy- just skip ahead that many letters. If its more variable you'll need to look into regular expressions or other custom work. – Gabe Sechan Mar 11 '16 at 03:21
  • You might find [setTag](http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object)) useful. – tachyonflux Mar 11 '16 at 03:23
  • I got the entire text of `TextView` using `return Html.fromHtml(html).toString();`. The problem is, "Check this out! " is just an example.. It is user typed and it can and will change according to the user.. I've gotten the Regex but don't know how to apply it to get the Youtube link.. – Kevin Murvie Mar 11 '16 at 03:23
  • How is `setTag` useful? – Kevin Murvie Mar 11 '16 at 03:24
  • can you please paste the string? – Abdul Aleem Mar 11 '16 at 03:31

4 Answers4

5

You can use regular expressions with the Patterns.WEB_URL regular expression to find all urls in your text.

Cullub
  • 2,901
  • 3
  • 30
  • 47
Rathod Vijay
  • 417
  • 2
  • 7
1

Try this method from @BullyWiiPlaza answer

public static String extractYouTubeUrl(String text)
{
    String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
    Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
    Matcher urlMatcher = pattern.matcher(text);

    while (urlMatcher.find())
    {
       String url = text.substring(urlMatcher.start(0),
                urlMatcher.end(0))
       if(url.contains("youtube.com")
        return url;
    }    
    return "";
}

How to use it:

String stringWithYouTubeUrl = tvWithYouTubeUrl.getText().toString();
String youTubeUrl = extractYouTubeUrl(stringWithYouTubeUrl);

Log.d(TAG, "youTubeUrl is" + youTubeUrl);

Hope it helps!!

Community
  • 1
  • 1
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
  • I'm using it in onBindViewHolder method inside `RecyclerView` which is void which means I can't use `return url`.. Out of desperation, I did `if(url.contains("youtube.com")) youtubeVidUrl = url;` but as expected, it says `youtubeVidUrl` might not have been initialized.. – Kevin Murvie Mar 11 '16 at 04:06
  • Or.. Getting each value of the list till it ends, it's impactful on performance though.. – Kevin Murvie Mar 11 '16 at 04:09
  • Umm please could you provide your `RecyclerView` so we can help you better...I'll edit my answer to fit your needs – Gueorgui Obregon Mar 11 '16 at 04:12
  • Modified stuffs from your link, basic is still the same, extract URLs, and then check whether the URLs contains youtube or not, and if it has, get the URL.. Thanks! :D – Kevin Murvie Mar 11 '16 at 04:36
  • Glad to have this problem fixed (bugs may remain though) in only about an hour hahaha! :D – Kevin Murvie Mar 11 '16 at 04:38
0

You can try it:

String textViewStr = "Check this out! (Youtube link)";
int pos_1 = textViewStr.indexOf('(');
int pos_2 = textViewStr.indexOf(')');

String result = textViewStr.substring(pos_1+1,pos_2);
System.out.println(result); // print: Youtube link

UPDATE:

String textViewStr = "asdf Check this out! https://www.youtube.com/ asdg asfgf https:!!";
String [] arrStr = textViewStr.split(" ");
String regex = "/(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=‌​|watch\?.+&v=))((\w|-){11})(?:\S+)?$/g";
Pattern p = Pattern.compile(regex,Pattern.DOTALL);

        for(int i = 0 ; i < arrStr.length ; ++i){
            Matcher m = m = p.matcher(arrStr[i]);
            if ( m.matches() )
                Log.d("Matcher", "PATTERN MATCHES! " + arrStr[i]);
            else
                Log.d("MATCHER", "PATTERN DOES NOT MATCH! : " + arrStr[i]);
        }

This print:

D/MATCHER: PATTERN DOES NOT MATCH! : asdf
D/MATCHER: PATTERN DOES NOT MATCH! : Check
D/MATCHER: PATTERN DOES NOT MATCH! : this
D/MATCHER: PATTERN DOES NOT MATCH! : out!
D/Matcher: PATTERN MATCHES! https://www.youtube.com/
D/MATCHER: PATTERN DOES NOT MATCH! : asdg
D/MATCHER: PATTERN DOES NOT MATCH! : asfgf
D/MATCHER: PATTERN DOES NOT MATCH! : https:!!s
fechidal89
  • 723
  • 5
  • 25
  • `String textViewStr = "Check this out! https://www.youtube.com/watch?v=MxX0a8UdjuI";` now just find the HTTPS position it may solve right? @KevinMurvie – Rasa Mohamed Mar 11 '16 at 03:50
  • @KevinMurvie See the update. You can't search https because this: "https is a secure protocol. see the https at https:////www.youtube.com/...." – fechidal89 Mar 11 '16 at 04:05
  • This is my regex /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/g – Kevin Murvie Mar 11 '16 at 04:21
  • You change the "String regex" by your regex string and this should work. Check the answer edit. It is not necessary the while on strings that are urls. – fechidal89 Mar 11 '16 at 11:48
0

The YouTube link syntax is https://www.youtube.com/watch?v=<value>. what you really need is v=<value>. just search whole string for "watch?" if there is v= after that then just take the value and create the whole url from it.

Ankit
  • 429
  • 3
  • 16
  • I got the value if the `TextView` contains only Youtube link by using a method called `extractYoutubeId` from an answer in SO – Kevin Murvie Mar 11 '16 at 03:46
  • use String.indexOf("watch?"); to get the index of "watch?" in the whole string. then check if there is v= after it, if it does then just get the value after "=". – Ankit Mar 11 '16 at 04:05