onlyGreek method Null safety

String? onlyGreek()

Returns only the Greek characters from the String.

Example

String foo = '4*%^55/σοφ4e5523ια';
String onlyGreek = foo.onlyGreek(); // returns 'σοφια'
String foo2 = '4*%^55/σοφ4e5523ια aaggαγάπ112η';
String onlyGreek2 = foo2.onlyGreek(); // returns 'σοφια αγάπη'

Implementation

String? onlyGreek() {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }
  // ignore: unnecessary_raw_strings
  var regex = RegExp(r'([^α-ωΑ-ΩίϊΐόάέύϋΰήώΊΪΌΆΈΎΫΉΏ\s]+)');
  return this!.replaceAll(regex, '');
}