C# 如何使用正则表达式c替换部分文本#
例如:C# 如何使用正则表达式c替换部分文本#,c#,regex,string,C#,Regex,String,例如: string str = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > A worker can be of 3 different types.</ p > <end> < p ></ p > </ body > </ h
string str = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > A worker can be of 3 different types.</ p > <end> < p ></ p > </ body > </ html > "
string replacement = "hello world";
string newString = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > hello world</ p > <end> < p ></ p > </ body > </ html > "
string str=$@“头1一个工人可以有3种不同的类型。”
字符串替换=“hello world”;
字符串newString=$@“头1你好世界”
所以我有一个
和
符号来知道文本的哪一部分应该被替换。如何通过regex获取新闻字符串。使用regex.Replace
可以将模式从第一个
设置为第二个,包括中间的所有模式。然后指定要替换的内容
var result = Regex.Replace(str, "<start>.*?<end>", $"<start> {replacement} <end>");
var result=Regex.Replace(str,“.*”,$”{replacement});
如果在C#6.0字符串插值之前,则:
var result = Regex.Replace(str, "<start>.*?<end>", string.Format("<start> {0} <end>",replacement));
var result=Regex.Replace(str,“.*”,string.Format(“{0}”,replacement));
使用注释中的最新字符串:
string str = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > A worker can be of 3 different types.</ p > <end> < p ></ p > </ body > </ html > ";
string replacement = "hello world";
var result = Regex.Replace(str, "<start>.*?<end>", $"<start> {replacement} <end>");
string str=$@“头1一个工人可以有3种不同的类型;
字符串替换=“hello world”;
var result=Regex.Replace(str,“.*”,$“{replacement}”);
使用Regex.Replace
将模式从第一个
设置为第二个,包括中间的所有模式。然后指定要替换的内容
var result = Regex.Replace(str, "<start>.*?<end>", $"<start> {replacement} <end>");
var result=Regex.Replace(str,“.*”,$”{replacement});
如果在C#6.0字符串插值之前,则:
var result = Regex.Replace(str, "<start>.*?<end>", string.Format("<start> {0} <end>",replacement));
var result=Regex.Replace(str,“.*”,string.Format(“{0}”,replacement));
使用注释中的最新字符串:
string str = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > A worker can be of 3 different types.</ p > <end> < p ></ p > </ body > </ html > ";
string replacement = "hello world";
var result = Regex.Replace(str, "<start>.*?<end>", $"<start> {replacement} <end>");
string str=$@“头1一个工人可以有3种不同的类型;
字符串替换=“hello world”;
var result=Regex.Replace(str,“.*”,$“{replacement}”);
这对于正则表达式来说太简单了。使用更有效的String.Replace()方法。@DoniyorNiyozov-它是什么意思“没有帮助”?这对我很管用……你用的是什么C?如果在c#6.0下,请参阅更新您使用了惰性计算器(?
),它与可选的多个*
,将匹配0个字符。您需要*
@IsaacvanBakel-它可以与?
:)配合使用-这适用于以后他在字符串中有更多
的情况。然后我要最小匹配。测试过了吉拉德·格林:帖子上没有写“中间人”。你在假设什么。我以为那个人只是想被替换。这对正则表达式来说太简单了。使用更有效的String.Replace()方法。@DoniyorNiyozov-它是什么意思“没有帮助”?这对我很管用……你用的是什么C?如果在c#6.0下,请参阅更新您使用了惰性计算器(?
),它与可选的多个*
,将匹配0个字符。您需要*
@IsaacvanBakel-它可以与?
:)配合使用-这适用于以后他在字符串中有更多
的情况。然后我要最小匹配。测试过了吉拉德·格林:帖子上没有写“中间人”。你在假设什么。我以为那个人只是想被替换。