Flutter Dart:如何使用RegEx从字符串中提取数字 [英] Flutter Dart: How to extract a number from a string using RegEx

查看:1843
本文介绍了Flutter Dart:如何使用RegEx从字符串中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to extract a number (integer, decimal or 12:30 formats) from a string. I have used the following RegEx but to no avail:

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]", "");

到目前为止,没有什么能完美地工作。

So far, nothing works perfectly.

感谢任何帮助。

推荐答案

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)));
}

这篇关于Flutter Dart:如何使用RegEx从字符串中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆