简体   繁体   English

如何用.net中的新String替换第n个正则表达式组?

[英]How do I replace the nth regex group with a new String in .net?

I have a pipe delimited string like this: 我有一个像这样的管道分隔字符串:

blah|blah|blah|blah|blah|blah|blah|blah|blah|oldDate|blah|blah| 等等|等等|等等|等等|等等|等等|等等|等等|等等| oldDate |等等|等等|

I would like replace the content of the 10th section with a new Date. 我想用新的日期替换第10部分的内容。 I am able to match the the old date with the 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 can't figure out how to repace that group with new text. 但是,我无法弄清楚如何用新文本重新组织该组。 I have tried: 我试过了:

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

But that returns the original message like it was unable to find the 4th group. 但这会返回原始消息,就像它无法找到第4组一样。 I am unable to do a string replace with the matched date since there are multiple dates in the string (orderDate, receivedDate, etc) and its possible to match on one of those by accident. 我无法用匹配的日期替换字符串,因为字符串中有多个日期(orderDate,receivedDate等),并且可能偶然匹配其中一个日期。

Does anyone know how to replace this text? 有谁知道如何替换这个文字?

Thanks in advance for any help. 在此先感谢您的帮助。

There's no apparent reason to use a regex here, use strMessage.Split('|') . 这里没有明显的理由使用正则表达式,使用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 do need to use a regex, consider using Regex.Replace(string, string) : 如果确实需要使用正则表达式,请考虑使用Regex.Replace(string, string)

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

Replace works the other way around - you use groups to keep tokens in your string, not to move them out. 各地更换工程其他方式-您使用组来保持令牌的字符串,而不是将它们搬走。

So what you are doing is this, you are calling a Shared version of the RegEx library on your regex, you aren't actually using your regex to find the match, it is using the "$4" to find a match, which it isn't. 所以你正在做的就是这个,你在你的正则表达式上调用RegEx库的Shared版本,你实际上并没有使用你的正则表达式找到匹配,它是使用"$4"找到一个匹配,它不是“T。 This is why you are getting your original string back. 这就是为什么要取回原始字符串的原因。

I would do something like this to get the appropriate group replaced with the new date 我会做这样的事情,以便用新的日期替换相应的组

Dim matchedGroups As New System.Text.RegularExpressions.Matches = pipeRegEx.Matches(strMessage)

matchedGroups(9) = "new Date"

And then just put your list back together. 然后将您的列表重新组合在一起。

You're going about it backward. 你要落后了。 What you want to do is capture everything preceding the tenth field and then plug it back in along with the new value. 你想要做的是捕获第十个字段之前的所有内容,然后将其与新值一起重新插入。 You regex looks good, so you just have to change the replacement parameter. 你的正则表达式看起来很好,所以你只需要更改替换参数。 Something like this: 像这样的东西:

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

(You have to plug the | back in as well, because the regex consumes it.) (你必须插回| ,因为正则表达式会消耗它。)

you could use a 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

The Replace method is static . Replace方法static This means that it's not considering the fact that you called it on theRegEx object pipeRegEx and instead looking for the literal string "$4". 这意味着它没有考虑你在theRegEx对象pipeRegEx上调用它而是查找文字字符串“$ 4”的事实。

I would rewrite my expression using a zero-width look-behind (?<=expression) and zero-width look-ahead (?=expression) so that it only matched the 9th item (and leave it as a string instead of a RegEx). 我会使用零宽度后视(?<=expression)和零宽度前瞻(?=expression)重写我的表达式,以便它只匹配第9项(并将其保留为字符串而不是RegEx )。 Then you would call: 然后你会打电话:

RegEx.Replace(strMessage,pipeRegEx, newDate);

RegEx syntax resource: http://www.regular-expressions.info/reference.html RegEx语法资源: http//www.regular-expressions.info/reference.html

Ok, I was able to get the solution with the help of Brad's answer and a different way to get the solution with Kobi's answer, however I felt Brad's was more succinct. 好吧,我能够在布拉德的答案的帮助下获得解决方案,并通过不同的方式获得Kobi答案的解决方案,但我觉得布拉德更简洁。

'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 & "|").

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM