Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dart RegEx is not splitting String

Im a fresher to RegEx.
I want to get all Syllables out of my String using this RegEx:
/[^aeiouy]*[aeiouy]+(?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi

And I implemented it in Dart like this:

void main() {
  String test = 'hairspray';
  final RegExp syllableRegex = RegExp("/[^aeiouy]*[aeiouy]+(?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi");
  print(test.split(syllableRegex));
}

The Problem:
Im getting the the word in the List not being splitted.
What do I need to change to get the Words divided as List.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tested the RegEx on regex101 and it shows up to Matches.
But when Im using it in Dart with firstMatch I get null

>Solution :

You need to

  • Use a mere string pattern without regex delimiters in Dart as a regex pattern
  • Flags are not used, i is implemented as a caseSensitive option to RegExp and g is implemented as a RegExp#allMatches method
  • You need to match and extract, not split with your pattern.

You can use

String test = 'hairspray';
final RegExp syllableRegex = RegExp(r"[^aeiouy]*[aeiouy]+(?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?",
                caseSensitive: true);
for (Match match in syllableRegex.allMatches(test)) {
   print(match.group(0));
}

Output:

hair
spray
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading