Davy de Vries
Davy de Vries

Reputation: 5123

Trying to parse numbers with regex

I'm trying to parse all numbers from a text:

text 2030 text 2,5 text 2.000.000 2,000,000 -200 +31600000000. 200. 2.5 200? 1:200

Based on this regex:

(?<!\S)(\-?|\+?)(\d*\.?\,?\d+|\d{1,3}(,?.?\d{3})*(\.\,\d+)?)(?!\S)

But endings like ., ?, !, , right after the number doesn't match. I only want full matches with preg_match_all. (see image)

Overview of matches

I guess that the problem is in the last part of my regex (?!\S). I tried different things but I can't figured it out how to solve this.

Upvotes: 0

Views: 74

Answers (1)

Emma
Emma

Reputation: 27723

If we don't wish to validate our numbers, maybe we could then start with a simple expression, maybe something similar to:

(?:^|\s)([+-]?[\d:.,]*\d)\b

Test

$re = '/(?:^|\s)([+-]?[\d:.,]*\d)\b/s';
$str = 'text 2030 text 2,5 text 2.000.000 2,000,000 -200 +31600000000. 200. 2.5 200? 1:200
';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches);

In the right panel of this demo, the expression is further explained, if you might be interested.


EDITS:

Another expression would be:

(?:^|\s)\K[+-]?(?:\d+:\d+|\d+(?:[.,]\d{1,3})+|\d+)\b

which would not still validate our numbers and just collect those listed, with some invalid numbers.

DEMO 2

DEMO 3

Upvotes: 4

Related Questions