regex - Dart : How to extract a number from a string using RegEx

标签 regex dart flutter

我想从字符串中提取数字(整数、小数或 12:30 格式)。我使用了以下 RegEx 但无济于事:

final RegExp numberExp = new RegExp(
      "[a-zA-Z ]*\\d+.*",
      caseSensitive: false,
      multiLine: false
    );
final RegExp numberExp = new RegExp(
      "/[+-]?\d+(?:\.\d+)?/g",
      caseSensitive: false,
      multiLine: false
    );
String result = value.trim();
result = numberExp.stringMatch (result);
result = result.replaceAll("[^0-9]", "");
result = result.replaceAll("[^a-zA-Z]", "");

到目前为止,没有什么是完美的。

感谢任何帮助。

最佳答案

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 a
23.4567
type 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)));
}

关于regex - Dart : How to extract a number from a string using RegEx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53618776/

相关文章:

r - 匹配字符范围内的元素 n 次

flutter - 使用手势动态调整位置大小和旋转 Flutter 小部件

flutter - 在 flutter 中调用 null 方法 'toDouble()'

android - Flutter - 将 #(哈希)和 @(提及)符号添加到 native 键盘

android - 如何解决 SocketException : Failed host lookup: 'www.xyz.com' (OS Error: No address associated with hostname, errno = 7)

dart - 如何修复 "pub get failed(1)"

ruby-on-rails - WebSockets : Can connect but cannot subscribe to channel

c - 正则表达式 (regex) 中的点

java - 附加到数字的字符的正则表达式为 "1a"

c# 正则表达式匹配排除第一个和最后一个字符