Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to regex (1.2.3)?

Tags:

regex

php

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.

like image 651
Lee Loftiss Avatar asked Feb 10 '23 17:02

Lee Loftiss


1 Answers

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+\)
like image 96
Pruthvi Raj Avatar answered Feb 12 '23 12:02

Pruthvi Raj