Home > Software engineering >  flutter compare check if two urls are similar
flutter compare check if two urls are similar

Time:11-19

I would like to compare two url links in flutter and see if they are similar then return a true value. An example is comparing the below links

https://stackoverflow.com/
https://stackoverflow.com/questions/ask

CodePudding user response:

I would do it like this:

var link1 = 'https://stackoverflow.com/';
var link2 = 'https://stackoverflow.com/questions/ask';

print(link2.contains(link1)); => it will return true

CodePudding user response:

This stackoverflow answer shows regex for domain name matching. You can use it like this:

  final domainRegex = RegExp(r"^(?:https?:\/\/)?(?:[^@\n] @)?(?:www\.)?([^:\/\n?] )");

  var firstUrl = 'https://stackoverflow.com/';
  var secondUrl = 'https://stackoverflow.com/questions/ask';

  var firstDomain = domainRegex.firstMatch(first_url)?.group(1);   //stackoverflow.com
  var secondDomain = domainRegex.firstMatch(second_url)?.group(1);   //stackoverflow.com

  print(firstDomain == secondDomain);  //returns true
  • Related