import Foundation
let pattern = ##"""
<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}
.*
"""##
let regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .allowCommentsAndWhitespace, .dotMatchesLineSeparators])
let testString = #"""
<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>
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"MNO1=$1 , MNO2=$2"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression