Use regex :
(\d+(?:\.\d+)*)
Structure:
\d+ find one or more digits\. literal decimal character .\d+ followed by one or more digits.(...)* this means 0 or more occurrences of this pattern match(?:...) this tells the engine not to create a backlink for this group (basically we donβt use the link, so itβs pointless to have it)
You did not provide much information about the data, so I made the following assumptions:
- Data will always contain at least one number
- Data can only contain a number without a period
- Data may contain multiple digits
- The numbers themselves can contain any number of pairs of dots / numbers
If any of these assumptions is incorrect, you will have to change the regular expression.
Usage example:
$x = "OP/1.1.2/DIR"; if (!preg_match('/(\d+(\.\d+)*)/', $x, $matches)) { // Could not find a matching number in the data - handle this appropriately } else { var_dump($matches[1]); // string(5) "1.1.2" }
source share