[英]Replace values in string
I am looking to find matches in a string, perform an action on the match, and then replace the original match. 我正在寻找在字符串中找到匹配项,对该匹配项执行操作,然后替换原始匹配项。
For example finding @yahoo in a string, looking on matching everything after the ampersand to the first white space. 例如,在字符串中找到@yahoo,寻找将与号后面的所有内容都匹配到第一个空格。 Of course there can be multiple values to match on in a single string so would be a for each match. 当然,在单个字符串中可以有多个匹配的值,因此每个匹配都将有一个。
I'm thinking regex but am not sure on matching on everything after the ampersand to the first white space (the regex expression for this?). 我正在考虑使用正则表达式,但是不确定是否将与号后面的所有内容都匹配到第一个空格(为此使用正则表达式吗?)。 Or any other easier way of doing this? 或任何其他更简单的方式做到这一点?
For this: 为了这:
looking on matching everything after the ampersand to the first white space 寻找将与号后的所有内容匹配到第一个空格
regexp is @\\S+
. regexp是@\\S+
。
Reference: Character Classes . 参考: 字符类 。
Assuming you have your Regex correctly setup, you can utilize one of the overloads of Regex.Replace to include a MatchEvaluator delegate. 假设您正确设置了Regex,则可以利用Regex.Replace的重载之一来包含MatchEvaluator委托。 The MatchEvaluator
is a Func<Match,string>
delegate (meaning any public string Method(Match match)
method will work as input), with the return value being what you want to replace the original string with. MatchEvaluator
是Func<Match,string>
委托(意味着任何public string Method(Match match)
方法都将用作输入),返回值是您想要替换原始字符串的值。 The regex for the search is (@\\S+)
which means "Match the @
symbol, followed by any non-whitespace character ( \\S
) at least once ( +
). 搜索的正则表达式为(@\\S+)
,表示“匹配@
符号,后接任何非空白字符( \\S
)至少一次( +
)。
Regex.Replace(input, "(@\S+)", (match) => { /* Replace logic here. */ })
Running the above regex on the input @yahoo.com is going to be @simple for purposes of @matching.
@yahoo.com is going to be @simple for purposes of @matching.
在输入@yahoo.com is going to be @simple for purposes of @matching.
上运行上述正则表达式@yahoo.com is going to be @simple for purposes of @matching.
, it matches on @yahoo.com
, @simple
and @matching.
,它与@yahoo.com
, @yahoo.com
@simple
和@matching.
(notice that it includes punctiation on the @matching.
). (请注意,它包括@matching上的@matching.
)。
Hope that helps! 希望有帮助!
If you're writing in C#, a regex is probably your best option. 如果您使用C#编写,则正则表达式可能是您的最佳选择。 The code is quite simple 代码很简单
MatchCollection matches = Regex.Matches(/*input*/, /*pattern*/)
foreach (Match m in matches)
{
/*Do work here*/
}
For learning regular expressions and the syntax associated, I used http://www.regular-expressions.info/tutorial.html to get started. 为了学习正则表达式和相关的语法,我使用http://www.regular-expressions.info/tutorial.html入门。 A lot of good information in there, and easy to read. 那里有很多很好的信息,而且易于阅读。
For example: 例如:
string str = "@yahoo aaaa bbb";
string replacedStr = str.Replace("@yahoo", "replacement");
Look to documentation: string.Replace 查看文档: string.Replace
Do you mean ampersand &
or at-symbol @
? 你的意思是符号&
或符号@
?
This should do what you need: &([\\S\\.]+)\\b
这应该可以满足您的需求: &([\\S\\.]+)\\b
or for the at-symbol: @([\\S\\.]+)\\b
或符号符号: @([\\S\\.]+)\\b
Try using String.Replace() function: 尝试使用String.Replace()函数:
String x="lalala i like being @Yahoo , my Email is John@Yahoo.com";
x=x.Replace("@Yahoo","@Gmail");
X will be now : "lalala i like being @Gmail, my Email is John@Gmail.com"; X现在是:“拉拉拉,我喜欢当@Gmail,我的电子邮件是John@Gmail.com”;
To know where the next space after "@Yahoo", use location variable, with String.IndexOf() and String.LastIndexOf(). 要知道“ @Yahoo”之后的下一个空格,请使用具有String.IndexOf()和String.LastIndexOf()的位置变量。
int location=x.IndexOf("@Yahoo");//gets the location of the first "@Yahoo" of the string.
int SpaceLoc=x.IndexOf("@Yahoo",location);// gets the location of the first white space after the first "@Yahoo" of the string.
Hope that helps. 希望能有所帮助。
I think a RegEx.Replace is your best bet. 我认为RegEx.Replace是您最好的选择。 You can simply do something like this: 您可以简单地执行以下操作:
string input = "name@yahoo.com is my email address";
string output = Regex.Replace(input, @"@\S+", new MatchEvaluator(evaluateMatch));
And you just need to define the evaluateMatch method, such as: 而且,您只需要定义validateMatch方法,例如:
private string evaluateMatch(Match m)
{
switch(m.Value)
{
case "@yahoo.com":
return "@google.com";
break;
default:
return "@other.com";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.