通过RegEx替换未知字符串 [英] replace unknown string via RegEx
问题描述
我正在尝试使用常规
表达式替换未知字符串。
例如我有:
user_pref(" network.proxy.http"," server1")
我该怎么办?必须做的是替换server1部分(可以是
其他任何东西,例如test2),带有自定义字符串。
Hi,
i''m currently trying to replace an unknown string using regular
expressions.
For example I have:
user_pref("network.proxy.http", "server1")
What do I have to do to replace the "server1" part (which could be
anything else e.g. "test2") with a custom string.
推荐答案
< ja ******* @ gmx.de>在消息中写道
news:11 ********************** @ i40g2000cwc.googlegr oups.com ...
<ja*******@gmx.de> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
我正在尝试使用常规
表达式替换未知字符串。
例如我有:
user_pref(" network.proxy.http"," server1")
我需要做些什么才能更换server1"使用自定义字符串的部分(可能是其他任何内容,例如test2)。
Hi,
i''m currently trying to replace an unknown string using regular
expressions.
For example I have:
user_pref("network.proxy.http", "server1")
What do I have to do to replace the "server1" part (which could be
anything else e.g. "test2") with a custom string.
私有字符串ReplaceProxyValue(字符串输入,字符串ReplaceWith)
{
string pattern =
"(?< A> user_pref \\(\" network\\。 proxy \\.http\",\")" +
"(?< Value>。+)(?< Z> \" \\ \))" ;;
string input =" user_pref(\" network.proxy.http\",\" server1 \" )" ;;
string replacePattern ="
private string ReplaceProxyValue(string Input, string ReplaceWith)
{
string pattern =
"(?<A>user_pref\\(\"network\\.proxy\\.http\", \")" +
"(?<Value>.+)(?<Z>\"\\))";
string input = "user_pref(\"network.proxy.http\", \"server1\")";
string replacePattern = "
{A}" + ReplaceWith +"
{A}" + ReplaceWith + "
{Z}";
if(Regex.IsMatch(input,pattern)){
返回Regex.Replace(input,pattern,replacePattern);
} else {
返回输入;
}
}
HTH :)
Mythran
{Z}";
if (Regex.IsMatch(input, pattern)) {
return Regex.Replace(input, pattern, replacePattern);
} else {
return Input;
}
}
HTH :)
Mythran
这篇关于通过RegEx替换未知字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!