metzelder
metzelder

Reputation: 734

Php preg_match_all function to find a version pattern

I am trying to get the version info from a file that contains the line:

[assembly: AssemblyVersion("1.0.0.1")]

I need to get 1.0.0.1

The function i have tried something like following:

preg_match_all('/[assembly: AssemblyVersion("([^,">]+).*?")/')

But no matches returned with this function.

What is the correct way to get version info in this example?

Upvotes: 0

Views: 132

Answers (3)

Slava
Slava

Reputation: 938

If you can solve problem without regex - you have to do it. Because this functions are very heavy and unpredictable .

For example, just split string by double quotes (if string has a strict format):

$parts = explode('"', '[assembly: AssemblyVersion("1.0.0.1")]');
echo $parts[1];

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522741

Your current code does not appear to even be working. The main problem, from what I can see, is that you are not escaping literal parentheses and square brackets. After making that change, it seems to work:

$input = "blah blah blah [assembly: AssemblyVersion(\"1.0.0.1\")] blah";
preg_match_all('/\[assembly: AssemblyVersion\("([^,">]+).*?"\)\]/', $input, $array);
print_r($array[1]);

Array
(
    [0] => 1.0.0.1
)

Upvotes: 2

HamzaNig
HamzaNig

Reputation: 1029

You can use this regex :

$line='[assembly: AssemblyVersion("1.0.0.1")]';

    preg_match_all("/(?<=\[assembly: AssemblyVersion\(\").*(?=\"\))]/",$line,$matches);

Upvotes: 1

Related Questions