flutter_parsed_text
flutter_parsed_text copied to clipboard
Some regex patterns are not working
Hi, There are some pattern issues i couldnt figure it out myself.
this is working as expected:
final str = "--- spoiler ---\r\n\r\n spoiler content \r\n--- spoiler ---\r\n\r";
Iterable<Match> matches = RegExp(
r"(---( )?(`)?spoiler(`)?( )?---)(.*?)(---( )?(`)?spoiler(`)?( )?---)",
dotAll: true,
multiLine: true,
caseSensitive: false)
.allMatches(str);
matches.forEach((m) => print(m.group(6)));
// output: "spoiler content"
however this is not working. renderText method is not called:
Widget build(BuildContext context) {
return ParsedText(
text: str,
style: TextStyle(color: Colors.black),
parse: [
MatchText(
type: ParsedType.CUSTOM,
pattern:
r"(---( )?(`)?spoiler(`)?( )?---)(.*?)(---( )?(`)?spoiler(`)?( )?---)",
regexOptions: RegexOptions(
dotAll: true,
multiLine: true,
caseSensitive: false,
),
style: TextStyle(
color: Colors.red,
fontSize: 10,
),
renderText: ({String str, String pattern}) {
Map<String, String> map = Map<String, String>();
Match match = RegExp(pattern).firstMatch(str);
map['display'] = "(${match.group(6).trim()})";
map['value'] = match.group(6).trim();
return map;
},
onTap: (url) {
print(url);
}),
],
);
same goes for positive lookbehind this is working:
final str =
"(lookup: testtest1) content content (lookup: testtest2) content content";
Iterable<Match> matches =
RegExp(r"(?<=\(lookup:)(.*?)(?=\))").allMatches(str);
matches.forEach((m) => print(m.group(0)));
// output: testtest1
// output: testtest2
this is not:
Widget build(BuildContext context) {
return ParsedText(
text: str,
style: TextStyle(color: Colors.black),
parse: [
MatchText(
type: ParsedType.CUSTOM,
pattern:
r"(?<=\(lookup:)(.*?)(?=\))",
style: TextStyle(
color: Colors.red,
fontSize: 10,
),
renderText: ({String str, String pattern}) {
Map<String, String> map = Map<String, String>();
Match match = RegExp(pattern).firstMatch(str);
map['display'] = "(${match.group(2).trim()})";
map['value'] = match.group(2).trim();
return map;
},
onTap: (url) {
print(url);
}),
],
);
im using flutter_parsed_text: ^1.2.3
sdk environment for flutter sdk: ">=2.5.2 <3.0.0"
@ksmsk I don't why it's not working but I will take a look.
@fayeed I have a similar issue using ParsedType.CUSTOM. The pattern is only recognized when the string does contain other words besides the expression. As can be seen in the screenshot below there is no issue when using the ParsedType.URL, but there is using my custom IP address pattern.

The code below shows what I assign to the parse attribute of the ParsedText widget to get my example:
List<MatchText> get parsePatterns => <MatchText>[
// MatchText for url patterns
MatchText(
type: ParsedType.URL,
style: _urlStyle,
onTap: (String url) async {
String _url = url.toLowerCase();
// If the url is of type ParsedType.URL but cannot be launched add https://
if (!(_url.contains('https://') || _url.contains('http://')) &&
!(_url.contains('mailto:') ||
_url.contains('tel:') ||
_url.contains('sms:'))) {
_url = 'http://' + _url;
}
// Try to launch the url using `package:url_launcher`
if (await canLaunch(_url)) {
await launch(_url);
} else {
throw 'Could not launch $_url';
}
}),
// MatchText for IP addresses
MatchText(
type: ParsedType.CUSTOM,
regexOptions: RegexOptions(caseSensitive: false),
pattern:
r"^(?:http|https):\/\/[\w\-_]+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",
style: _urlStyle,
onTap: (url) => print(url))
];
+1