【发布时间】:2020-07-03 23:42:16
【问题描述】:
我想从 Html 字符串中提取电子邮件地址。首先,我从 html 字符串中提取文本,然后将返回的字符串传递给正则表达式以搜索电子邮件地址。
import 'package:html/parser.dart';
import 'package:flutter/material.dart';
String _parseString(String htmlString) {
var document = parse(htmlString);
String parsedString = parse(document.body.text).documentElement.text;
return parsedString;
}
String _parseEmailString(String htmlString) {
var document = parse(htmlString);
String parsedString = parse(document.body.text).documentElement.text;
RegExp exp = new RegExp(
r"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$",
caseSensitive: false,
multiLine: false,
);
Iterable<RegExpMatch> matches = exp.allMatches('WS://127.0.0.1:56789');
print("hasMatch : " + exp.firstMatch("shahryar.rsm@gmail.com").toString());
var matches2 = "hasMatch : " + exp.stringMatch(parsedString).toString();
return matches2;
}
class HtmlToText extends StatelessWidget {
@override
Widget build(BuildContext context) {
String string =
'<p style=\"text-align: center\"><strong>The CV Club</strong> is a professional writing service that specialise in tailoring CVs, cover letters and more to your desired industry/ job role.</p>\n<p style=\"text-align: center\">The internet is full of dull CV templates, all exactly the same as each other. That’s why at <strong>The CV Club</strong>, we professionally tailor your CV to be as individual as you are and not at extortionate prices!</p>\n<p style=\"text-align: center\">We offer a FREE CV review and will give you constructive feedback on your current CV!</p>\n<p>Contact us on;</p>\n<p><strong>Website: <a href=\"https://www.thecvclub.org/\">https://www.thecvclub.org/</a></strong></p>\n<p><strong>Email: thecvclub@gmail.com</strong></p>\n<p><strong>Twitter: thecvclub_</strong></p>\n<p><img data-attachment-id=\"1055\" data-permalink=\"https://soleentrepreneur.co.uk/happy-world-book-day/\" data-orig-file=\"https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?fit=800%2C800&ssl=1\" data-orig-size=\"800,800\" data-comments-opened=\"1\" data-image-meta=\"{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}\" data-image-title=\"Happy World Book Day!\" data-image-description=\"\" data-medium-file=\"https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?fit=300%2C300&ssl=1\" data-large-file=\"https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?fit=800%2C800&ssl=1\" class=\"alignnone size-medium wp-image-1055 alignleft\" src=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day-300x300.png?resize=300%2C300\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=300%2C300&ssl=1 300w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=80%2C80&ssl=1 80w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=768%2C768&ssl=1 768w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=36%2C36&ssl=1 36w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=180%2C180&ssl=1 180w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=705%2C705&ssl=1 705w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=120%2C120&ssl=1 120w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=450%2C450&ssl=1 450w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=640%2C640&ssl=1 640w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?resize=50%2C50&ssl=1 50w, https://i2.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Happy-World-Book-Day.png?w=800&ssl=1 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" /> <img data-attachment-id=\"1056\" data-permalink=\"https://soleentrepreneur.co.uk/lets-celebrate/\" data-orig-file=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?fit=800%2C800&ssl=1\" data-orig-size=\"800,800\" data-comments-opened=\"1\" data-image-meta=\"{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}\" data-image-title=\"Let’s celebrate\" data-image-description=\"\" data-medium-file=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?fit=300%2C300&ssl=1\" data-large-file=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?fit=800%2C800&ssl=1\" class=\"alignnone size-medium wp-image-1056 alignleft\" src=\"https://i0.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate-300x300.png?resize=300%2C300\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=300%2C300&ssl=1 300w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=80%2C80&ssl=1 80w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=768%2C768&ssl=1 768w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=36%2C36&ssl=1 36w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=180%2C180&ssl=1 180w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=705%2C705&ssl=1 705w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=120%2C120&ssl=1 120w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=450%2C450&ssl=1 450w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=640%2C640&ssl=1 640w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?resize=50%2C50&ssl=1 50w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Lets-celebrate.png?w=800&ssl=1 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" /> <img data-attachment-id=\"1059\" data-permalink=\"https://soleentrepreneur.co.uk/need-help-with-your-cv_/\" data-orig-file=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?fit=800%2C800&ssl=1\" data-orig-size=\"800,800\" data-comments-opened=\"1\" data-image-meta=\"{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}\" data-image-title=\"Need help with your CV_\" data-image-description=\"\" data-medium-file=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?fit=300%2C300&ssl=1\" data-large-file=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?fit=800%2C800&ssl=1\" class=\"alignnone size-medium wp-image-1059 alignleft\" src=\"https://i0.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_-300x300.png?resize=300%2C300\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=300%2C300&ssl=1 300w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=80%2C80&ssl=1 80w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=768%2C768&ssl=1 768w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=36%2C36&ssl=1 36w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=180%2C180&ssl=1 180w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=705%2C705&ssl=1 705w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=120%2C120&ssl=1 120w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=450%2C450&ssl=1 450w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=640%2C640&ssl=1 640w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?resize=50%2C50&ssl=1 50w, https://i1.wp.com/soleentrepreneur.co.uk/wp-content/uploads/2019/02/Need-help-with-your-CV_.png?w=800&ssl=1 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" /></p>\n<p> </p>\n';
return Scaffold(
appBar: AppBar(),
body: Column(children: <Widget>[
SelectableText(
_parseString('$string'),
),
Text(_parseEmailString('$string'))
]));
}
}
【问题讨论】:
标签: regex flutter parsing dart