I'm trying to make utility function to parse a string and return a list of regex matches. The string that returns from my server looks something like this:
{{Yoga dance class}}
{{Herzog, Walter and Glover}}
{{Yoga, dance}}
{{Intermediate}}
I'm trying to match the regex like this:
class ParsedClassDescription {
final String userFacingDescription;
final String? organization;
final String? categories;
final String? level;
const ParsedClassDescription({
required this.userFacingDescription,
this.organization,
this.categories,
this.level,
});
}
ParsedClassDescription? parseClassDescription(String? description) {
if (description == null) {
return null;
}
final regex = RegExp(r'/(?<={{\s*).*?(?=\s*}})/gs');
final match = regex.allMatches(description);
return const ParsedClassDescription(userFacingDescription: 'test test ignore this');
}
I'm not exactly sure if this RegExp works... am I doing something wrong?
CodePudding user response:
As this comment by Günter Zöchbauer says, remove the '/' from the beginning of the string. The answer that is a comment on points out that you don't need the '/g' (.allMatches()
performs that function). I ran your regex in DartPad and got it to return matches by removing both of those characters and the 's':
final regex = RegExp(r'(?<={{\s*).*?(?=\s*}})');
final match = regex.allMatches("{{Yoga dance class}} {{Herzog, Walter and Glover}} {{Yoga, dance}} {{Intermediate}}");