通过RegEx替换未知字符串 [英] replace unknown string via RegEx

查看:101
本文介绍了通过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屋!

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