-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
88 lines (71 loc) · 2.19 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import 'package:text_parser/text_parser.dart';
Future<void> main() async {
const text = '''
abc https://example.com/sample.jpg. def
john.doe@example.com 911 +1-012-3456-7890
01111111111 tel:02222222222
<a class="bar" href="https://example.com/">
Content inside tags
</a>
''';
// Using preset matchers
var parser = TextParser(
matchers: const [
EmailMatcher(),
UrlMatcher(),
TelMatcher(),
],
);
var elements = await parser.parse(text);
elements.forEach(print);
print('-' * 20);
// Extracting only phone number elements as an Iterable
final telElements = elements.whereMatcherType<TelMatcher>();
telElements.forEach(print);
print('-' * 20);
// Replacing matchers with new ones
parser.matchers = const [TelMatcher(r'(?<=tel:)\d{11}')];
elements = await parser.parse(text);
elements.forEach(print);
print('-' * 20);
// Obtaining only matching elements
elements = await parser.parse(text, onlyMatches: true);
elements.forEach(print);
print('-' * 20);
// Multiple matchers of the same type
parser = TextParser(
matchers: const [
PatternMatcher('Pattern A'),
PatternMatcher('Pattern B'),
],
);
elements = await parser.parse('Pattern A & Pattern B');
elements.forEach(print);
print('-' * 20);
// Extracting only elements resulting from the matcher at a particular index
final bElements = elements.whereMatcherType<PatternMatcher>(matcherIndex: 1);
bElements.forEach(print);
print('-' * 20);
// Capturing unnamed and named groups
parser = TextParser(
matchers: const [
PatternMatcher(r'(?<year>\d{4})-(\d{2})-(?<day>\d{2})'),
],
);
elements = await parser.parse('2020-01-23', onlyMatches: true);
elements.forEach(print);
print('-' * 20);
// Custom matcher for <a> tags
// (`dotAll` is enabled to make '.' match line endings too)
parser = TextParser(matchers: const [ATagMatcher()], dotAll: true);
elements = await parser.parse(text, onlyMatches: true);
elements.forEach(print);
}
class ATagMatcher extends TextMatcher {
const ATagMatcher()
: super(
r'\<a\s(?:.+?\s)*?href="(.+?)".*?\>'
r'\s*(.+?)\s*'
r'\</a\>',
);
}