如何使用通配符替换字符串中的字符串 [英] How to replace charecters in a string with wildcard

查看:104
本文介绍了如何使用通配符替换字符串中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想用一个?替换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

推荐答案

那是因为''\d''是rexeg -speak for a digit:''0''''9'',因为你的字符串包含abcdefgh而且根本没有数字,所以它永远不会匹配。



尝试

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}"

七个alphnumerics,或:

for seven alphnumerics, or:

@"DATE=[a-zA-Z]{7}"

七个大写或小写字母。



获取<的副本a href =http://www.ultrapico.com/Expresso.htm> Expresso [ ^ ] - 它是免费的,它会检查并生成正则表达式。

for seven upper or lower case alphabetics.

Get a copy of Expresso [^] - it''s free, and it examines and generates Regular expressions.


这篇关于如何使用通配符替换字符串中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆