问题描述
我想用一个?替换20120101。但是,我搜索的20120101字符串并不总是一样的。它始终以2开头,并且始终包含8个字符。它可能是20121225,20130510等。
我有以下代码,它对整数有效。
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.
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.
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
推荐答案
@"DATE=.{7}"
任意角色中的七个,或者:
for seven of any character, or:
@"DATE=\w{7}"
七个alphnumerics,或:
for seven alphnumerics, or:
@"DATE=[a-zA-Z]{7}"
七个大写或小写字母。
获取 Expresso [] - 它是免费的,它会检查并生成正则表达式。
for seven upper or lower case alphabetics.
Get a copy of Expresso [^] - it''s free, and it examines and generates Regular expressions.
这篇关于如何使用通配符替换字符串中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!