How is regex (1.2.3)?

I need to search for documents for some text in this format:

(#.#.#) ex; (1.4.6)

      

As simple as it sounds, this is beyond my regex skills.

+3


source to share


2 answers


You can use the following regex:

\(\d{1,2}\.\d{1,2}\.\d{1,2}\)

      

Regular expression visualization

DEMO

PHP example:



<?php
$str = "(1.12.12) some text (1.1.1) some other text (1.1232.1) text";
preg_match_all('/\(\d{1,2}\.\d{1,2}\.\d{1,2}\)/',$str,$matches);
print_r($matches);
?>

      

Output:

Array
(
    [0] => Array
        (
            [0] => (1.12.12)
            [1] => (1.1.1)
        )

)

      

If you want to have any number of digits (> 0) use the following regex:

\(\d+\.\d+\.\d+\)

      

+7


source


Have you tried this?

$int = preg_match("/\(\d{1,2}\.\d{1,2}\.\d{1,2}\)/", "(11.2.33)", $matches);

      



You can test it here http://micmap.org/php-by-example/en/function/preg_match

+1


source







All Articles