Lee Loftiss
Lee Loftiss

Reputation: 3195

How to regex (1.2.3)?

I need to search documents for a bit of text with this format:

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

As simple as this may appear, it is outside my regex skills.

Upvotes: 4

Views: 1215

Answers (2)

Pruthvi Raj
Pruthvi Raj

Reputation: 3036

You can use the following regex:

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

Regular expression visualization

DEMO

Sample PHP:

<?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 can have any number of digits (>0) , use following regex:

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

Upvotes: 7

mcorne
mcorne

Reputation: 77

Did you try 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

Upvotes: 1

Related Questions