Dart: RegExp by example Dart: RegExp by example dart dart

Dart: RegExp by example


Try this regex:

String regex = "^http://myapp.example.com/#([^?]+)";

And then grab: matches.group(1)


The RegExp class comes with a convenience method for a single match:

RegExp regExp = new RegExp(r"^http://myapp.example.com/#([^?]+)");var match = regExp.firstMatch("http://myapp.example.com/#fizz?a=1");print(match[1]);

Note: I used anubhava's regular expression (yours was not escaping the ? correctly).

Note2: even though it's not necessary here, it is usually a good idea to use raw-strings for regular expressions since you don't need to escape $ and \ in them. Sometimes using triple-quote raw-strings are convenient too: new RegExp(r"""some'weird"regexp\$""").


String regex = "^http://myapp.example.com/#([^?]+)";

Then:

var match = matches.elementAt(0);print("${match.group(1)}"); // output : fizz