11

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.

Any help appreciated.

4 Answers 4

11
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)));
}
Sign up to request clarification or add additional context in comments.

2 Comments

hello,why this fails when the the integer is like this570009, I mean when there is no space between the String and the int, as well as why does this only work when the string starts from 2nd line?
Because the regexp in my answer specifically skips only spaces at the beginning/end (\s). You probably want something like r'.*?(\d*).*' or r'[^0-9]*([0-9]*).*'. I haven't tested these examples and I don't do Regexp much, so be wary. (only on my phone)
4

For one-line strings you can simply use:

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

Comments

3

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]

Comments

-1

Try this for phone numbers starting with +country code detection in a multiline string.

\b[+][(]{0,1}[6-9]{1,4}[)]{0,1}[-\s.0-9]\b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.