Kevnlan
Kevnlan

Reputation: 567

flutter compare check if two urls are similar

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

Upvotes: 0

Views: 424

Answers (2)

Mecid X Recebli
Mecid X Recebli

Reputation: 93

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

Upvotes: 0

kmtz
kmtz

Reputation: 544

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

Upvotes: 2

Related Questions