Home > OS >  How to identify a domain
How to identify a domain

Time:12-22

you need to do something similar:

Strting str = 'google.com'; //it will be a dynamic string

if(str = domain){
//Some method
}else{}

How to determine whether String is the site domain or just text? I am making a browser string

CodePudding user response:

We can validate given String is URL or not in dart by below,

bool _validURL = Uri.parse(str).isAbsolute; //true valid URL

or

Uri.tryParse(str)?.hasAbsolutePath ?? false; //true valid url

or by RegEx

var urlPattern = r"(https?|http)://([-A-Z0-9.] )(/[-A-Z0-9 &@#/%=~_|!:,.;]*)?(\?[A-Z0-9 &@#/%=~_|!:‌​,.;]*)?";
var match = new RegExp(urlPattern, caseSensitive: false).firstMatch(str);
match = RegExp(urlPattern, caseSensitive: false).firstMatch(str);

or

String myURL = "google.com";
var matchCaseOne = new RegExp("^(http[s]?:\\/\\/(www\\.)?|ftp:\\/\\/(www\\.)?|www\\.){1}([0-9A-Za-z-\\.@:%_\ ~#=] ) ((\\.[a-zA-Z]{2,3}) )(/(.)*)?(\\?(.)*)?", caseSensitive: false).firstMatch(myURL);
var matchCaseTwo = new RegExp("^([0-9A-Za-z-\\.@:%_\ ~#=] ) ((\\.[a-zA-Z]{2,3}) )(/(.)*)?(\\?(.)*)?", caseSensitive: false).firstMatch(myURL);
if(matchCaseOne !=null || matchCaseTwo !=null){
  print("valid URL");
}else{
  print("Not Valid URL");
}

//tested below URL's

https://google.com
http://google.com
ftp://google.com
www.google.com
https://www.google.com
http://www.google.com
ftp://www.google.com
google.com
  • Related