正则表达式问题 - 替换模式 [英] Regular expression problem - Replacing a pattern

查看:96
本文介绍了正则表达式问题 - 替换模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个文本文件,我加载到一个字符串。该文本包括

我希望匹配的{firstName}或{userName}等特定表达式

然后用新表达式替换。但是,我想使用括号中包含的

文本进行查找,这样我就可以用新文本替换

表达式。


例如:


假设文本文件为:

Hello {firstName},您的帐户{accountName}已更新。


我需要弄清楚如何编写表达式,以便字符串

读取如下:

Hello Dimitris,您的帐户Hotmail已更新。


更换后。请记住,我需要使用括号中包含的值

,以便我可以查找

相应替换的内容。


任何帮助将不胜感激。


Dimitris

Hello,

I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However, I want to use the
text included within the brackets to do a lookup so that I can replace
the expression with the new text.

For example:

Lets say the text file reads:
Hello {firstName}, Your account {accountName} has been updated.

I need to figure out how to write the expression so that the string
reads like:
Hello Dimitris, Your account Hotmail has been updated.

after the replacement. Keep in mind that I need to use the value
contained in the brackets so that I can do a lookup to figure what the
corresponding replacement is.

Any help would be appreciated.

Dimitris

推荐答案

进行简单的更换像这样你可以使用String.Relace()功能

,无需使用正则表达式。


字符串模板=" Hello {firstName},您的帐户{accountName}已经

已更新。" ;;

String name =" Dimitris";

String account =" Hotmail";

String message = " ;;


message = template.Replace(" {firstName}",name);

message = message.Replace(" {accountName}",account);


Console.WriteLine(" String Replate:" + message);


//替代方法:正则表达式

//因为{和}是控制字符,你必须使用\

message = Regex.Replace(模板,@" \ {firstName} \}",name);

message = Regex.Replace(message,@" \ {accountName \}",account);


Console.WriteLine(" Regex Replate:" + message);

for simple replacement like this you can use "String.Relace()" function
without having to use Regex.

String template = "Hello {firstName}, Your account {accountName} has been
updated.";
String name = "Dimitris";
String account = "Hotmail";
String message = "";

message = template.Replace("{firstName}", name);
message = message.Replace("{accountName}", account);

Console.WriteLine("String Replate: " + message);

// Alternative: regex
// since { and } are control chars u have to escape using \
message = Regex.Replace(template, @"\{firstName\}", name);
message = Regex.Replace(message, @"\{accountName\}", account);

Console.WriteLine("Regex Replate: " + message);

替换后。请记住,我需要使用括号中包含的值
,以便我可以查找
相应替换的内容。


我不知道这里有什么意思,你能解释一下吗


无论如何希望有所帮助,


Du


" Dimitris Georgakopuolos" <二******* @ hotmail.com>在消息中写道

新闻:ea ************************** @ posting.google.c om ...您好,

我有一个文本文件,我加载到一个字符串。该文本包括某些表达,如{firstName}或{userName}我想要匹配
然后用新表达式替换。但是,我想使用括号中包含的
文本进行查找,以便我可以用新文本替换表达式。

例如:

让我们说文本文件是:
Hello {firstName},您的帐户{accountName}已更新。

我需要弄清楚如何编写表达式,以便字符串
如下:
您好Dimitris,您的帐号Hotmail已更新。

更换后。请记住,我需要使用括号中包含的值
,以便我可以查找
相应的替换是什么。

任何帮助将不胜感激。

Dimitris
after the replacement. Keep in mind that I need to use the value
contained in the brackets so that I can do a lookup to figure what the
corresponding replacement is.
i''m not sure what mean here, can you explain it a little more

anyway hope that helps,

Du

"Dimitris Georgakopuolos" <di*******@hotmail.com> wrote in message
news:ea**************************@posting.google.c om... Hello,

I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However, I want to use the
text included within the brackets to do a lookup so that I can replace
the expression with the new text.

For example:

Lets say the text file reads:
Hello {firstName}, Your account {accountName} has been updated.

I need to figure out how to write the expression so that the string
reads like:
Hello Dimitris, Your account Hotmail has been updated.

after the replacement. Keep in mind that I need to use the value
contained in the brackets so that I can do a lookup to figure what the
corresponding replacement is.

Any help would be appreciated.

Dimitris



如果您需要在某处查找,可以随时使用MatchEvaluator。

在这种情况下匹配的任何内容都将被发送到一个函数,您可以返回

替换结果。如果匹配是firstName,那么你可以用

替换某个用户的名字

。如果匹配是accountName,请进行查找并使用
替换它。


听起来你想要一个通用的标签替换方法和MatchEvaluator

正是你想要的那种情况。查看 www.regexlib.com 。一个朋友
我的b $ b运行网站,他们有大量正则表达式的东西。

-

贾斯汀罗杰斯

DigiTec Web Consultants,LLC。

博客: http: //weblogs.asp.net/justin_rogers


" Du Dang" < 6 ******* @ hotmail.com>在消息中写道

新闻:Ix ******************** @ nnrp1.uunet.ca ...
If you need to do a look-up somewhere, you can always use a MatchEvaluator.
Whatever is matched in this case will be sent to a function where you can return
the replacement result. If the match is firstName, then you can replace that
with
some user''s first name. If the match is accountName, do your look-ups and
make the replacement for that.

It sounds like you want a generic tag replacement method and the MatchEvaluator
is exactly what you''d want in that case. Check out www.regexlib.com. A friend
of mine runs the site and they have loads of regular expressions stuff going on.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Du Dang" <vi*******@hotmail.com> wrote in message
news:Ix********************@nnrp1.uunet.ca...
对于像这样的简单替换,你可以使用String.Relace()功能
无需使用正则表达式。

字符串模板=" Hello {firstName},您的帐户{accountName}已更新。" ;;
字符串name =" Dimitris";
String account =" Hotmail";
String message ="" ;;

message = template.Replace(" {firstName} ",name);
message = message.Replace(" {accountName}",account);

Console.WriteLine(" String Replate:" + message);

//替代方法:正则表达式
//因为{和}是控制字符,你必须使用\
message = Regex.Replace(模板,@" \ {firstName \}",name);
message = Regex.Replace(message,@" \ {accountName \}",account);

Console.WriteLine (Regex Replate:+消息);

for simple replacement like this you can use "String.Relace()" function
without having to use Regex.

String template = "Hello {firstName}, Your account {accountName} has been
updated.";
String name = "Dimitris";
String account = "Hotmail";
String message = "";

message = template.Replace("{firstName}", name);
message = message.Replace("{accountName}", account);

Console.WriteLine("String Replate: " + message);

// Alternative: regex
// since { and } are control chars u have to escape using \
message = Regex.Replace(template, @"\{firstName\}", name);
message = Regex.Replace(message, @"\{accountName\}", account);

Console.WriteLine("Regex Replate: " + message);

更换后。请记住,我需要使用括号中包含的值
,以便我可以查找
相应替换的内容。
after the replacement. Keep in mind that I need to use the value
contained in the brackets so that I can do a lookup to figure what the
corresponding replacement is.



我我不确定这里有什么意思,你能解释一下吗

无论如何希望有所帮助,

Dimitris Georgakopuolos ; <二******* @ hotmail.com>在消息中写道
新闻:ea ************************** @ posting.google.c om ...



i''m not sure what mean here, can you explain it a little more

anyway hope that helps,

Du

"Dimitris Georgakopuolos" <di*******@hotmail.com> wrote in message
news:ea**************************@posting.google.c om...

你好,

我有一个文本文件,我加载到一个字符串。该文本包括某些表达,如{firstName}或{userName}我想要匹配
然后用新表达式替换。但是,我想使用括号中包含的
文本进行查找,以便我可以用新文本替换表达式。

例如:

让我们说文本文件是:
Hello {firstName},您的帐户{accountName}已更新。

我需要弄清楚如何编写表达式,以便字符串
如下:
您好Dimitris,您的帐号Hotmail已更新。

更换后。请记住,我需要使用括号中包含的值
,以便我可以查找
相应的替换是什么。

任何帮助将不胜感激。

Dimitris
Hello,

I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However, I want to use the
text included within the brackets to do a lookup so that I can replace
the expression with the new text.

For example:

Lets say the text file reads:
Hello {firstName}, Your account {accountName} has been updated.

I need to figure out how to write the expression so that the string
reads like:
Hello Dimitris, Your account Hotmail has been updated.

after the replacement. Keep in mind that I need to use the value
contained in the brackets so that I can do a lookup to figure what the
corresponding replacement is.

Any help would be appreciated.

Dimitris




Du,

$ b $我感谢你的帮助。我很抱歉没有明确第一个

时间。我要做的是检索值动态地进行

替换,因此我不提前知道

设计时间的字段类型要查看对于;它可能是

{firstName}或{lastName}或100个其他我不想要的字段

硬代码。


我正在寻找一个这样的例子来表示日期,并希望为我的场景做一些类似的事情,但我不确定最好的方法来获得它b



返回Regex.Replace(输入,

" \\b(?< month> \\d {1,2 })/(?< day> \\\\ {1,2})/(?< year> \\\\ {2,4})\\b",

"
Du,

I appreciate your help. I apologize for not being clear the first
time around. What I am trying to do is retrieve the values to do the
replacement dynamically, therefore I do not know ahead of time in
design time of the type of fields to look for; it could be
{firstName} or {lastName} or 100 other fields that I don''t want to
hard code.

I was looking at an example like this for dates and want to do
something similar for my scenario but I am not sure of the best way to
accomplish it:

return Regex.Replace(input,
"\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b",
"


这篇关于正则表达式问题 - 替换模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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