How to replace the nth group of regular expressions with a new line in .net?

I have a delimited string similar to this line:

l | l | l | l | l | l | l | l | l | oldDate | l | whether |

I want to replace the contents of the 10th section with a new date. I can match the old date with the following code:

'create a group with the first 9 non-pipes followed by a pipe, a group with the old date followed by a pipe, and sub-group of just the old date. Dim pipeRegEx As New RegularExpressions.Regex("(([^\|]*\|){9})(([^\|]*)\|)") 'the sub-group is the 4th group ordinal in the match GroupCollection Dim oldDate as String=pipeRegEx.Match(strMessage).Groups(4).Value 

However, I cannot figure out how to undo this group with new text. I tried:

 pipeRegEx.Replace(strMessage, "$4", newDate) 'where newDate is a string var 

But this returns the original message since the 4th group could not be found. I cannot replace a string with a matching date, because there are several dates in the string (orderDate, receivedDate, etc.) And it can be matched by one of them.

Does anyone know how to replace this text?

Thanks in advance for your help.

+4
source share
6 answers

Replace the method - static . This means that it does not take into account the fact that you called it on the RRExExRegEx object and instead looked for the letter string "$ 4".

I would rewrite my expression using look-behind (?<=expression) with zero width and the back with zero width (?=expression) so that only the 9th element matches (and leave it as a string instead of RegEx). Then you should call:

 RegEx.Replace(strMessage,pipeRegEx, newDate); 

RegEx Syntax Resource: http://www.regular-expressions.info/reference.html

0
source

There is no obvious reason to use regex, use strMessage.Split('|') .

 string str = "blah|blah|blah|blah|blah|blah|blah|blah|blah|oldDate|blah|blah"; string[] tokens = str.Split('|'); tokens[9] = "newDate"; string newStr = String.Join("|", tokens); 

If you need to use a regex, consider using Regex.Replace(string, string) :

 pipeRegEx.Replace(strMessage, "$2" & newDate & "|") 

Replace works the other way around - you use groups to store tokens in your string, and not to move them.

+3
source

So what do you do, you call the Shared version of the RegEx library in your regular expression, you don’t actually use your regular expression to find a match, it uses "$4" to search for a match that isn't there. That is why you are returning your original string.

I would do something similar to replace the corresponding group with a new date

 Dim matchedGroups As New System.Text.RegularExpressions.Matches = pipeRegEx.Matches(strMessage) matchedGroups(9) = "new Date" 

And then just return your list together.

+2
source

You go back about it. What you want to do is grab everything in front of the tenth field, and then connect it along with the new value. The regex looks good, so you just need to change the replacement parameter. Something like that:

 newString = pipeRegEx.Replace(strMessage, "$1" + newDate + "|") 

(You need to reconnect | because the regex consumes it.)

+2
source

you can use split

 Dim str As String = "blah|blah|blah|blah|blah|blah|blah|blah|blah|oldDate|blah|blah|" Dim obj As Object = str.Split("|") obj(9) = "new date" 

then

 str = strings.join(obj, "|") 

or

 for each obj1 as object in obj str&= obj1 next 
+1
source

Well, I was able to get a solution using Brad's answer and another way to get a solution with Kobe's answer, however, I felt that Brad was more concise.

 'This regex does a look behind for the string that starts with 9 sections 'and another look ahead for the next pipe. So the result of this regex is only 'the old date value. Dim pipeRegEx As New RegularExpressions.Regex("(?<=^(([^\|]*\|){9}))([^\|]*)(?=\|)") 'I then store the old date with: oldDate=pipeRegEx.Match(strMessage).Value 'And replace it with the new date: strMessage=pipeRegEx.Replace(strMessage, newDate & "|"). 
0
source

All Articles