Flutter Dart: How to extract a number from a string using RegEx Flutter Dart: How to extract a number from a string using RegEx dart dart

Flutter Dart: How to extract a number from a string using RegEx


const text = '''Lorem Ipsum is simply dummy text of the 123.456 printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an 12:30 unknown printer took a galley of type and scrambled it to make a23.4567type specimen book. It has 445566 survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.''';final intRegex = RegExp(r'\s+(\d+)\s+', multiLine: true);final doubleRegex = RegExp(r'\s+(\d+\.\d+)\s+', multiLine: true);final timeRegex = RegExp(r'\s+(\d{1,2}:\d{2})\s+', multiLine: true);void main() {  print(intRegex.allMatches(text).map((m) => m.group(0)));  print(doubleRegex.allMatches(text).map((m) => m.group(0)));  print(timeRegex.allMatches(text).map((m) => m.group(0)));}


That's how I solved my problem:

bool isNumber(String item){    return '0123456789'.split('').contains(item);}List<String> numbers = ['1','a','2','b','3','c','4','d','5','e','6','f','7','g','8','h','9','i','0'];print(numbers);numbers.removeWhere((item) => !isNumber(item));print(numbers);

And here's the output:

[1, a, 2, b, 3, c, 4, d, 5, e, 6, f, 7, g, 8, h, 9, i, 0][1, 2, 3, 4, 5, 6, 7, 8, 9, 0]


For one-line strings you can simply use:

final intValue = int.parse(stringValue.replaceAll(RegExp('[^0-9]'), ''));