$re = '~ <R-PORT-PROTOTYPE .*? <short-name>PQR</short-name>
(?:
(?:
(?(1) (?!) )
.*?
<V>
( .*? ) # (1)
</V>
.*?
<PARAMETER-REF [ ] DEST="PARAMETER-DATA-PROTOTYPE"> .*? MNO1</PARAMETER-REF>
)
|
(?:
(?(2) (?!) )
.*?
<V>
( .*? ) # (2)
</V>
.*?
<PARAMETER-REF [ ] DEST="PARAMETER-DATA-PROTOTYPE"> .*? MNO2</PARAMETER-REF>
)
){1,2}
.*
~xsi';
$str = ' <R-PORT-PROTOTYPE **************>
<SHORT-NAME>PQR</SHORT-NAME>
<REQUIRED-COM-SPECS>
<PARAMETER-REQUIRE-COM-SPEC>
<APPLICATION-VALUE-SPECIFICATION>
<SHORT-LABEL>abc1</SHORT-LABEL>
<SW-VALUES-PHYS>
<V>80</V>
</SW-VALUES-PHYS>
</APPLICATION-VALUE-SPECIFICATION>
<PARAMETER-REF DEST="PARAMETER-DATA-PROTOTYPE">**************/MNO1</PARAMETER-REF>
</PARAMETER-REQUIRE-COM-SPEC>
<PARAMETER-REQUIRE-COM-SPEC>
<APPLICATION-VALUE-SPECIFICATION>
<SHORT-LABEL>abc2</SHORT-LABEL>
<SW-VALUES-PHYS>
<V>-80</V>
</SW-VALUES-PHYS>
</APPLICATION-VALUE-SPECIFICATION>
<PARAMETER-REF DEST="PARAMETER-DATA-PROTOTYPE">**************/MNO2</PARAMETER-REF>
</PARAMETER-REQUIRE-COM-SPEC>
</REQUIRED-COM-SPECS>
</R-PORT-PROTOTYPE>
';
$subst = "MNO1=$1 , MNO2=$2";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php