简体   繁体   English

正则表达式来修改字符串

[英]regular expression to modify a string

I have a string that I need to modify.我有一个需要修改的字符串。 The part that I need to modify is the end of the string.我需要修改的部分是字符串的结尾。 It is something like "to 23 May 2017".它类似于“到 2017 年 5 月 23 日”。 I just need to change the date that it is stored.我只需要更改它的存储日期。

So I tried something like this:所以我尝试了这样的事情:

var texto = System.Text.RegularExpressions.Regex.Replace(test, "to "+@"^\d{1,2}\/\?[A-z]?\/\d{4}","to { refundEnd: dd MMM yyyy}");

However, there is a problem with the regular expression because I am not matching the date.但是,正则表达式有问题,因为我没有匹配日期。 I am using the 'to' because there is another date in the string but this one does not need to be modified.我使用“to”是因为字符串中有另一个日期,但不需要修改这个日期。

Any ideas?有任何想法吗?

Regex is only really useful in this instance if you actually want to do something with the date before you replace it (for example, the new date will be based on the current date).正则表达式仅在这种情况下真正有用,如果您确实想在替换日期之前对其进行某些操作(例如,新日期将基于当前日期)。 However, this doesn't seem to be the case based on your question.但是,根据您的问题,情况似乎并非如此。

If your date format is always going to be the same length, then you could always just trim off x characters from the end.如果您的日期格式总是相同的长度,那么您总是可以从末尾剪掉x字符。 But your current attempt suggests that the day part of the date will be unknown length (either one or two digits).但是您当前的尝试表明日期的日期部分将是未知长度(一位或两位数字)。 Based on that I would suggest the best approach would be to use the position of "to" to identify what should be removed.基于此,我建议最好的方法是使用"to"的位置来确定应该删除的内容。

You could do this 2 different ways, take your pick:您可以通过 2 种不同的方式执行此操作,请选择:


The SPLIT method分割方法

Split the string on "to" and then rebuild the new string:"to"上拆分字符串,然后重建新字符串:

string[] values = test.Split("to");
string result = string.Format("{0} to {1}", values[0].Trim(), newDateString);

The SUBSTRING method SUBSTRING 方法

Create a substring based on the last index of "to" (we use last index in case there are any other matches earlier on in the string) which will provide you everything before "to" .根据"to"的最后一个索引创建一个子字符串(我们使用最后一个索引,以防字符串中前面有任何其他匹配项),它将为您提供"to"之前的所有内容。 Then you can append the rest on to the end:然后你可以将其余的附加到最后:

string result = string.Format("{0} to {1}", test.Substring(0, test.LastIndexOf("to")).Trim(), newDateString);

In the end, what it worked for me was this:最后,它对我有用的是:

var texto = System.Text.RegularExpressions.Regex
        .Replace(test, @"to\s+(\d{1,2})\s+([A-z]+)\s+(\d{4})", "to " 
         + refundEnd.ToString("dd MMM yyyy"));

It is a combination of two issues:这是两个问题的组合:

1.- getting the proper regular expression. 1.- 获得正确的正则表达式。 2.- formatting the new date. 2.- 格式化新日期。

Thanks everyone for the help!感谢大家的帮助!

干得好。

var texto = System.Text.RegularExpressions.Regex.Replace(test, @"to\s+(\d{1,2})\s+([A-z]+)\s+(\d{4})","to { refundEnd: $1 $2 $3}");

You can just remove the last 11 characters and concatenate the new date you want to suffix it with.您可以只删除最后 11 个字符并连接要为其添加后缀的新日期。

 //Your New Date string
 String yourDate = "dd MMM YYYY";
 var texto = test.Remove(test.Length - 11, 11)+yourDate;

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

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