redanimalwar
redanimalwar

Reputation: 1523

PHP - Stuck with this regex

<?php

echo preg_replace( '/( \* Version:[ ]+)([0-9\.]{6})/',  '${1}' . '6.6.6' , ' * Version:           3.1.0' );

I basically want to detect the line with the Version and replace it with a new version. I not get why it is not working

Upvotes: 0

Views: 51

Answers (1)

raina77ow
raina77ow

Reputation: 106375

This subexpression...

([0-9\.]{6})

... will only match if there's exactly 6 target characters. But there's just 5 of them in 3.1.0 string (3 digits, two dots).

The easiest way to fix it is to change that subexpression to...

([0-9.]{1,6})

... so it'll match up to 6 characters. I've also removed the preceding backslash here: you don't need to escape . symbol within a character class (it doesn't have any special meaning here).

Upvotes: 2

Related Questions