I’m pulling out my hair over the following function:
Public Function SetVersion(ByVal hl7Message As String, ByVal newVersion As String) As String
Dim rgx = New Regex("^(?<pre>.+)(\|\d\.\d{1,2})$", RegexOptions.Multiline)
Dim m = rgx.Match(hl7Message)
Return rgx.Replace(hl7Message, "${pre}|" & newVersion, 1, 0)
End Function
For simplicity, I’m testing against the following input:
dsfdsaf|2.1
wretdfg|2.2
sdafasd3|2.3
What I need to accomplish is replace “|2.1” in the first line with another value, say “|2.4”. What is happening instead is that “|2.3” is getting replaced in the last line. It’s as if I hadn’t specified Multi-Line mode. Moreover, the following online tool returned correct matches. So, anyone who can see a mistake in my regex or code, please point it out.
By specifying
$you are essentially matching the last occurrence at the end of the string. If you want to match the first occurrence, remove the$or specify that a newline is expected:or
Based on your comment about using Multiline and appearance of your test data I imagine your input is on multiple lines. Use the above pattern and try this:
2.1 should change to 2.4.