skip to Main Content

I am trying to extract a link from an HTML body which is coming from a response . Then message looks like this "content":

        {
        "rendered": 
         "<div style=
           "padding: 56.25% 0 0 0; 
            position: relative;">
              <iframe style=
                 "position: absolute; 
                  top: 0; 
                  left: 0; 
                  width: 100%; 
                  height: 100%;" 
                  title="Shrewsberry" 
                  src="https://player.vimeo.com/video/1000224?h=23334&amp;badge=0&amp;autopause=0&amp;player_id=0&amp;app_id=58479" 
                  frameborder="0" 
                  allowfullscreen="allowfullscreen">
        </iframe>
        </div>"}

I want to get the content inside ‘src="https://player.vimeo.com/video/1000224?’ can anyone help .

This is my code which i use

final value = parse(html); // html is the value from response
String parsedString = parse(value.body!.text).documentElement!.text;
print(parsedString);

This is the results of what i got

2

Answers


  1. You can use Regex to extract the string from your original string. Given the fact that it looks like HTML I ignored the ** you put in your answer.

    In that case the regex (?<=src=").*?(?=") should work to retrieve the source. This will work for any source, not just Vimeo.

    import 'dart:convert';
    void main() {
      String input = ""content": { "rendered": "<div style="padding: 56.25% 0 0 0; position: relative;"><iframe style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" title="Shrewsberry" **src="https://player.vimeo.com/video/1000224?"h=23334&badge=0&autopause=0&player_id=0&app_id=58479" frameborder="0" allowfullscreen="allowfullscreen">n"}";
      
      RegExp regex = new RegExp('(?<=src=").*?(?=")');
      var match = regex.firstMatch(input);
      
      if (match == null) {
        print("No match found.");
        return;
      }
      
      print("Result: " + match.group(0)!);
    }
    

    Output: Result: https://player.vimeo.com/video/1000224?

    Login or Signup to reply.
  2. You can use this simple logic if your URL always ends with ?

    void main(){ 
    String x='"content": { "rendered": "<div style="padding: 56.25% 0 0 0; position: relative;"><iframe style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" title="Shrewsberry" **src="https://player.vimeo.com/video/1000224?**h=23334&badge=0&autopause=0&player_id=0&app_id=58479" frameborder="0" allowfullscreen="allowfullscreen">n"}';
    int start= x.indexOf('src');
    int end= x.indexOf('?');
    print(x.substring(start,end+1));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search