开发者

Regular Expression with Groups and Values in C#

开发者 https://www.devze.com 2022-12-11 14:35 出处:网络
I am trying to write a simple regex to convert some two digit years to four digit years in a pipe开发者_JS百科 delimited file.I am using:

I am trying to write a simple regex to convert some two digit years to four digit years in a pipe开发者_JS百科 delimited file. I am using:

Regex dateFormat = new Regex(@"\|(\d\d)/(\d\d)/([\d\d)\|");
string convertedString = dateFormat.Replace(contents, @"|$1$220$3|'");

What I want is |10/31/09| to be replaced with |10312009|.

What I am getting is |10$22009|

I think the problem is .NET is evaluating $1 and $3 but is thinking there is a group in the middle with no value ($220 maybe?). How can I let .NET know that the 20 is a constant value instead of part of the group value?

Thanks in advance


Your intuition about the problem is correct: the second backreference is being interpreted as $220, not $2. To fix this, use curly braces:

dateFormat.Replace(contents,@"|$1${2}20$3|'");

More info about .NET regular expressions is available here.


Your regex text doesn't parse. Was the "[" supposed to be there? Wrap the number in {} to fix the replace issue:

Regex dateFormat = new Regex(@"\|(\d\d)/(\d\d)/(\d\d)\|");
string convertedString = dateFormat.Replace(contents, @"|${1}${2}20${3}|'");


You can modify your Regex to use named groups instead. The syntax for a named group is (?). Then, in your Replace function you can use the group names instead of the group number.

Regex dateFormat = new Regex(@"\|(?<month>\d\d)/(?<day>\d\d)/(?<year>[\d\d)\|");
string convertedString = dateFormat.Replace(contents, @"|${month}${day}20${year}|'");


I don't know how to do that but here is my workaround. To use named group.

Regex dateFormat = new Regex(@"\|(?<month>\d\d)/(?<date>\d\d)/(?<year>\d\d)\|");
string convertedString = dateFormat.Replace(contents, @"|${month}${date}20${year}|'");

See more infor at the bottom of this page.

Hope this help.


Try this:

string contents = "|10/31/09|";
Regex dateFormat = new Regex(@"\|(?<mm>\d\d)/(?<dd>\d\d)/(?<yy>\d\d)\|");
Console.WriteLine(dateFormat.Replace(contents, "|${mm}${dd}20${yy}|"));

More information:

Call RegexObj.Replace("subject", "replacement") to perform a search-and-replace using the regex on the subject string, replacing all matches with the replacement string. In the replacement string, you can use $& to insert the entire regex match into the replacement text. You can use $1, $2, $3, etc... to insert the text matched between capturing parentheses into the replacement text. Use $$ to insert a single dollar sign into the replacement text. To replace with the first backreference immediately followed by the digit 9, use ${1}9. If you type $19, and there are less than 19 backreferences, the $19 will be interpreted as literal text, and appear in the result string as such. To insert the text from a named capturing group, use ${name}. Improper use of the $ sign may produce an undesirable result string, but will never cause an exception to be raised.

From http://www.regular-expressions.info/dotnet.html


I see problems with your regular expression, namely the unmatched [ character. The following works fine:

\|(?<month>\d{2})/(?<day>\d{2})/(?<year>\d{2})\|

That will group the month, day, and year results. You can then replace with the following string:

|$1/$2/20$3|
0

精彩评论

暂无评论...
验证码 换一张
取 消