Skip to content

Instantly share code, notes, and snippets.

@davenotik
Last active August 29, 2015 14:10
Show Gist options
  • Save davenotik/04235f61350b00911c4c to your computer and use it in GitHub Desktop.
Save davenotik/04235f61350b00911c4c to your computer and use it in GitHub Desktop.
class RegexHelper {
static String domain = '[a-zA-Z.]+';
static String protocol = '[a-zA-Z]+:\\/\\/';
static String www = 'www\\.';
static String emailName = '[a-zA-Z.+]+';
static String email = '\\b${emailName}@${domain}\\b';
static String queryPath = '\\/[-~+=%_a-zA-Z0-9.]+[-~+=%_a-zA-Z0-9]';
static String searchString = '\\?[-+=&;%@_.a-zA-Z0-9]+[-+=&;%@_a-zA-Z0-9]';
static String queryHash = '#[-=_a-zA-Z0-9]+';
static String link = '\\b($protocol|$protocol$www|$www)$domain($queryPath)*(\\/?$searchString)?($queryHash)?';
static String linkOrEmail = '($email|$link)';
}
main() {
var input = """
bar.ba@test.cokkkk <-- link
bar.ba@test.com <-- link
bar.+ba@test.com <-- link
bar.ba@google.ventures <-- link
Hello don't link this.
I am Dave.Noo I am not. <-- don't link this
whatever.com <-- don't link this, only if www and/or http:// precedes it
http://google.ventures <--- Allow new TLDs
www.demo.com <-- link
Have you checked http://foo.co.uk/? <-- don't pick up last ?
Have you checked http://foo.co.uk? <-- don't pick up last ?
Yes, I've checked http://foo.com. <-- don't pick up last .
http://regexr.com/foo.html?q=bar <-- link whole thing.
http://regexr.com/foo.html?q=bar. <-- don't pick up last .
http://regexr.com/foo.html?q=foo.bar. <-- don't pick up last .
http://regexr.com?q=foo.bar. <-- don't pick up last .
www.test.com/?asd=basd
www.test.com/?asd=basd#asd-sdf
ftp://whatever.com <-- link
abc://rad <-- fine to link
http://woven.app/asd?a
http://woven.app/asdasd/asda=sd=
This is a line.
And another line. <-- don't link the two lines.
Screenshot: http://d.pr/i/1kkDd
""";
print(RegexHelper.linkOrEmail);
new RegExp(RegexHelper.linkOrEmail).allMatches(input).forEach((Match m) {
print(m.group(1));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment