RegEx完全匹配

时间:2018-04-14 08:16:39

标签: regex dart

我需要检查输入字符串是否只有空格(只有空格),所以我写了下面的代码,但没有给出预期的答案,如果给出匹配结果,因为有空格,但它不应该因为那里是文中的其他字符串!

void main() {

  RegExp regExp = new RegExp(r"\s*");
  var input ="  h   ";
  var matches = regExp.stringMatch(input);

  if (matches != null)
    print('matches');
  else
    print('not matches');
}

1 个答案:

答案 0 :(得分:0)

尝试使用锚点覆盖整个字符串:

RegExp regExp = new RegExp(r"^\s*$");
var input ="  h   ";
var matches = regExp.stringMatch(input);

我不知道Dart,但我的猜测是RegExp#stringMatch默认情况下与整个字符串不匹配。