我有一串字符串,我正在使用一个Regex来根据需要替换不需要的字符。然而,我对删除日期有一个问题,例如: 1/09/2014 1/29或1-29。
我怎么才能把那些。我正在做这样的实验,但这是远远不够的:我可以简单地输入不起作用的单个字符。从这里拍摄:条形无效字符
Regex.Replace(strIn, @"[^\w\.@-]", "");样本输入将如下所示:2014年1月29日,我将去听音乐会。
输出:今天我要去听音乐会。
发布于 2014-01-29 07:48:28
这应该能行。为了生成它,我使用了http://txt2re.com/ ->非常方便的工具。
string txt="Today 01/29/2014 I will go to the concert";
string re1=".*?"; // Non-greedy match on filler
string re2="((?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])"; // MMDDYYYY 1
var newString = Regex.Replace(txt, re1+re2, "");发布于 2014-01-29 10:06:38
试试这个Regex
(?<=[0-9]{2}[\/\-][0-9]{2}[\/\-][0-9]{4}).*$|.*(?=[0-9]{2}[\/\-][0-9]{2}[\/\-][0-9]{4})REGEX演示
https://stackoverflow.com/questions/21424804
复制相似问题