Click here to Skip to main content
15,887,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to replace 20120101 with a ?. But, the 20120101 string I search for won't always be the same. It will always start with a 2, and always contain 8 characters. It may be 20121225, 20130510, etc.

I have the following code and its working fine to integers.

C#
string MyString ="SELECT Stuff FROM Table WHERE Code = Foo AND DATE=20120101";
                            string Fixed = Regex.Replace(MyString, @"DATE=2\d{7}", "DATE=?");



now i want to do the same for string.

C#
string MyString ="SELECT Stuff FROM Table WHERE Code = Foo AND DATE=abcdefgh";
                            string Fixed = Regex.Replace(MyString, @"DATE=a\d{7}", "DATE=?");


this code is not replacing the text.

please help me
Posted
Updated 8-Apr-13 3:32am
v3

1 solution

That's because '\d' is rexeg-speak for a digit: '0' to '9', Since your string contains "abcdefgh" and no digits at all, it will never match.

Try
@"DATE=.{7}"
for seven of any character, or:
@"DATE=\w{7}"
for seven alphnumerics, or:
@"DATE=[a-zA-Z]{7}"
for seven upper or lower case alphabetics.

Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
Ayyappan Ravi 8-Apr-13 9:40am    
Thanks, This is exactly what i want....
OriginalGriff 8-Apr-13 9:45am    
You're welcome!
Ayyappan Ravi 8-Apr-13 9:46am    
Hi,

Which will support all characters includes alphabets,numeric and special charecters.
OriginalGriff 8-Apr-13 10:29am    
'.' matches any single character, including decimals, alphas and special characters - seriously, get a copy of Expresso - it makes working them out a whole lot easier.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900