flutter如何从字符串中提取邮箱地址Email?

码云
2020-11-12 18:27

给定一个字符串,如何从中提取有效的email地址,参考以下代码:

void main() {
  //给定的字符串
  String s=r"""Good to know you!<br/><br/> <br/>Apply online today! 
  If you have any questions please contact fname@lname@company.com<br/>theUser will reach out if we believe you would be a good fit.<br/><br/>
  <br>111-222-3333</br>""";

  //email正则
  final mailPattern = r"\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b";

  final regEx = RegExp(mailPattern, multiLine: true);

  //正则匹配
  final obtainedMail = regEx.allMatches(s.toString()).map((m) => m.group(0)).join(' ');
  print(obtainedMail);
  
  //输出结果
  //lname@company.com
}

 

全部评论