Kevin Murvie
Kevin Murvie

Reputation: 2652

Android - Get specific text from TextView

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?

Upvotes: 0

Views: 1491

Answers (4)

Rathod Vijay
Rathod Vijay

Reputation: 417

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

Upvotes: 5

fechidal89
fechidal89

Reputation: 723

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

Upvotes: 0

Gueorgui Obregon
Gueorgui Obregon

Reputation: 5087

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!!

Upvotes: 1

Ankit
Ankit

Reputation: 429

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.

Upvotes: 0

Related Questions