2

I am trying to use regex to extract a certain syntax, in my case something like "10.100" or "20.111", in which 2 numbers are separated by dot(.) . So if I provide "a 10.100", it will extract 10.100 from the string. If I provide "a 10.100 20.101", it will extract 10.100 and 20.101.

Until now I have tried to use

preg_match('/^.*([0-9]{1,2})[^\.]([0-9]{1,4}).*$/', $message, $array);

but still no luck. Please provide any suggestion because I don't have strong regex knowledge. Thanks.

1
  • Please check my answer. If it does not work as expected, please add details to the question. Commented May 4, 2017 at 22:00

3 Answers 3

3

You may use

\b[0-9]{1,2}\.[0-9]{1,4}\b

See the regex demo.

Details:

  • \b - a leading word boundary
  • [0-9]{1,2} - 1 or 2 digits
  • \. - a dot
  • [0-9]{1,4} - 1 to 4 digits
  • \b - a trailing word boundary.

If you do not care about the whole word option, just remove \b. Also, to match just 1 or more digits, you may use + instead of the limiting quantifiers. So, perhaps

[0-9]+\.[0-9]+

will also work for you.

See a PHP demo:

$re = '/[0-9]+\.[0-9]+/';
$str = 'I am trying to use regex to extract a certain syntax, in my case something like "10.100" or "20.111", in which 2 numbers are separated by dot(.) . So if I provide "a 10.100", it will extract 10.100 from the string. If I provide "a 10.100 20.101", it will extract 10.100 and 20.101.';
preg_match_all($re, $str, $matches);
print_r($matches[0]);

Output:

Array
(
    [0] => 10.100
    [1] => 20.111
    [2] => 10.100
    [3] => 10.100
    [4] => 10.100
    [5] => 20.101
    [6] => 10.100
    [7] => 20.101
)
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a specific reason you don't use \d? Just out of curiosity.
Well, \d will match non-ASCII digits if /u modifier is used. I understand it is not the case here, but since \d may change the scope of chars it can match depending on a modifier, I'd rather use a verbose way to describe the chars to match. It is common to use [0-9] to avoid ambiguity.
Thanks, it works. do you mind explaining on which occasion we want to use \b?
The first \b is a word boundary, and it requires a start of string or a char other then a letter, digit or _ before the first digit matched with [0-9]{1,2}. The second \b requires the end of string or the character other than letter/digit/_ after the last digit matched with [0-9]{1,4}. If you do not use the word boundaries, you will match 12.3456 in 09876512.3456789999. If you plan to match 12.34 in abc12.34xyz you need to use (?<![0-9])[0-9]{1,2}\.[0-9]{1,4}(?![0-9]).
2

Regex: /\d+(?:\.\d+)/

1. \d+ for matching digits one or more.

2. (?:\.\d+) for matching digits followed by . like .1234

Try this code snippet here

<?php

ini_set('display_errors', 1);
$string='a 10.100 20.101';
preg_match_all('/\d+(?:\.\d+)/', $string, $array);
print_r($array);

Output:

Array
(
    [0] => Array
        (
            [0] => 10.100
            [1] => 20.101
        )

)

4 Comments

Your regex matches 2 or more dot separated digit sequences. OP only asks to match 2 dot separated digit sequences. Length limits are also removed from the regex, and although nothing is said about that, OP might want to filter out strings like 2345.334567.
@WiktorStribiżew Thanks. Got it. what you were trying to say, for that i have changed... My post..
Now it is the same as one of my suggestions. \d+\.\d+ is equal here (with no modifiers) to [0-9]+\.[0-9]+.
@WiktorStribiżew If i change this to [0-9]{2}\.[0-9]{3} then it might got wrong or match with some other.. Lets the OP decide which answer he likes, But your answer is best among once.. I know wiktor..
1
$decimals = "10.5 100.50 10.250";
preg_match_all('/\b[\d]{2}\.\d+\b/', $decimals, $output);
print_r($output);

Output:

Array
(
    [0] => 10.5
    [1] => 10.250
)

enter image description here

Regex Demo | Php Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.