'How to validate url using regex but without http and https

I want to validate url i am using this regex

!(Uri.tryParse(val)?.hasAbsolutePath ?? false)

i entered google.com but it return false when we do not add http or https if i use regex please provide me i dont know how to create regex for this



Solution 1:[1]

Here is the regex I came up with:

(?:https?://)?\S+\.\S+\.\S+

Regular expression visualization

Debuggex Demo

It does have a problem tho, it matches anything that isn't a whitespace, which means that something like this:

www.my/w(ebs"i%t&e.net

will be accepted. You can check this website if you want to look at better regexes for URLs

Here is how you can check a regex on dart:

RegExp myRegex = RegExp('my regex');

bool isURL(String url) {
  return myRegex.hasMatch(url);
}

Solution 2:[2]

Try installing the validators package and try isURL("google.com"). This helped me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 h8moss
Solution 2 My Car