0

I'm trying to fetch a specific part from the youtube URL. For example:- if the user has put the following URL https://www.youtube.com/watch?v=KOjE7cQ0FkA

I would fetch the following part KOjE7cQ0FkA using the below method

          String url = urlController.text; // https://www.youtube.com/watch?v=KOjE7cQ0FkA
          List urlList = url.split("=");
          String urlCode = await urlList[1];

but sometimes people copy the video link from mobile and this is how it would appear https://youtu.be/KOjE7cQ0FkA In such case, above code wouldn't work

So how can I detect which URL is put by the user and perform a split operation accordingly

Sorry, if my question sounds stupid but I hope you got an idea that what I'm trying to achieve here

1 Answer 1

1

There are many ways to approach this. One way would be to parse the URL (have a look at this SO question).

With this approach you could check if the query parameter watch is present -- if not the URL is probably in the mobile format.


Another approach would be to define a regex expression for the Video-ID (KOjE7cQ0FkA) part of the URL. That way you can extract the Video-ID regardless of the format of the URL. I would probably go with that approach.

Your regex could look like this: ([a-zA-Z]+(\d[a-zA-Z]+)+)

I used this site to create the regex. You probably need to modify it a little bit. Also if the ID has a fixed length that is a great criteria to filter by.

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

1 Comment

Thanks, mate! Accepted it as an answer :)

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.