如何操作C#中包含不同模式的字符串?
我有以下类型的字符串格式--- 该字符串包含一对位置不同的{},可以是n次。 现在,我想用其他字符串替换该对,我将根据{}对之间的字符串计算这些字符串如何操作C#中包含不同模式的字符串?,c#,string,C#,String,我有以下类型的字符串格式--- 该字符串包含一对位置不同的{},可以是n次。 现在,我想用其他字符串替换该对,我将根据{}对之间的字符串计算这些字符串 如何做到这一点 您可以通过“{”和“}”分割字符串,并以这种方式确定内容 但我认为更好的方法是按索引查找字符,然后知道一对或花括号的起始索引和结束索引,这样就可以在替换占位符的情况下重建字符串 但最好的方法可能是使用Regex.Replace,但这只会有助于将占位符替换为您想要的值,但我认为您的要求是还要解析花括号内的文本,并基于此选择要插入的值
如何做到这一点 您可以通过“{”和“}”分割字符串,并以这种方式确定内容 但我认为更好的方法是按索引查找字符,然后知道一对或花括号的起始索引和结束索引,这样就可以在替换占位符的情况下重建字符串
但最好的方法可能是使用Regex.Replace,但这只会有助于将占位符替换为您想要的值,但我认为您的要求是还要解析花括号内的文本,并基于此选择要插入的值,因此这可能不会很好地工作 你可以试试正则表达式。具体来说,
Regex.Replace
变量使用MatchEvaluator
应该可以做到这一点。有关更多信息,请参阅
大致如下:
using System;
using System.Text.RegularExpressions;
public class Replacer
{
public string Replace(string input)
{
// The regular expression passed as the second argument to the Replace method
// matches strings in the format "{value0#value1/value2}", i.e. three strings
// separated by "#" and "/" all surrounded by braces.
var result = Regex.Replace(
input,
@"{(?<value0>[^#]+)#(?<value1>[^/]+)/(?<value2>[^}]+)}",
ReplaceMatchEvaluator);
return result;
}
private string ReplaceMatchEvaluator(Match m)
{
// m.Value contains the matched string including the braces.
// This method is invoked once per matching portion of the input string.
// We can then extract each of the named groups in order to access the
// substrings of each matching portion as follows:
var value0 = m.Groups["value0"].Value; // Contains first value, e.g. "Jwala Vora"
var value1 = m.Groups["value1"].Value; // Contains second value, e.g. "3"
var value2 = m.Groups["value2"].Value; // Contains third value, e.g. "13"
// Here we can do things like convert value1 and value2 to integers...
var intValue1 = Int32.Parse(value1);
var intValue2 = Int32.Parse(value2);
// etc.
// Here we return the value with which the matching portion is replaced.
// This would be some function of value0, value1 and value2 as well as
// any other data in the Replacer class.
return "xyz";
}
}
public static class Program
{
public static void Main(string[] args)
{
var replacer = new Replacer();
var result = replacer.Replace("Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}");
Console.WriteLine(result);
}
}
使用系统;
使用System.Text.RegularExpressions;
公共类替换程序
{
公共字符串替换(字符串输入)
{
//正则表达式作为第二个参数传递给Replace方法
//匹配格式为“{value0#value1/value2}”的字符串,即三个字符串
//由“#”和“/”分隔,并用大括号包围。
var result=Regex.Replace(
输入
@"{(?[^#]+)#(?[^/]+)/(?[^}]+)}",
替代者(评价者);
返回结果;
}
专用字符串替换MatchEvaluator(匹配m)
{
//m.值包含匹配的字符串,包括大括号。
//此方法在输入字符串的每个匹配部分调用一次。
//然后,我们可以提取每个命名组,以便访问
//每个匹配部分的子字符串如下所示:
var value0=m.Groups[“value0”].Value;//包含第一个值,例如“Jwala Vora”
var value1=m.Groups[“value1”].Value;//包含第二个值,例如“3”
var value2=m.Groups[“value2”].Value;//包含第三个值,例如“13”
//在这里,我们可以将value1和value2转换为整数。。。
var intValue1=Int32.Parse(value1);
var intValue2=Int32.Parse(value2);
//等等。
//这里我们返回替换匹配部分的值。
//这将是value0、value1和value2以及
//Replacer类中的任何其他数据。
返回“xyz”;
}
}
公共静态类程序
{
公共静态void Main(字符串[]args)
{
var replacer=新的replacer();
var result=replacer.Replace(“由{MdOffice employee#1/1}向{Jwala Vora#3/13}提交{Amazon Vally#2/11}{1#3/75}的提案”);
控制台写入线(结果);
}
}
该程序将输出xyz向xyz提交的xyz xyz的建议
您需要在ReplaceMatchEvaluator
方法中提供特定于应用程序的逻辑,以酌情处理value0
、value1
和value2
。类Replacer
可以包含其他成员,这些成员可用于实现ReplaceMatchEvaluator
中的替换逻辑。通过对Replace
类的实例调用Replace
来处理字符串。您不能对string.Format()
执行某些操作吗
比如说
string.Format("Proposal is given to {0} for {1} {2} by {3}", "Jwala Vora", "Amazon Vally", 1, "MdOffice employee");
您可以使用该方法和{.*?}
模式。下面的示例使用字典替换值,但您可以使用自己的逻辑替换
class Program
{
static Dictionary<string, string> _dict = new Dictionary<string, string>();
static void Main(string[] args)
{
_dict.Add("{Jwala Vora#3/13}","someValue1");
_dict.Add("{Amazon Vally#2/11}", "someValue2");
_dict.Add("{1#3/75}", "someValue3");
_dict.Add("{MdOffice employee#1/1}", "someValue4");
var input = @"Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}";
var result = Regex.Replace(input, @"{.*?}", Evaluate);
Console.WriteLine(result);
}
private static string Evaluate(Match match)
{
return _dict[match.Value];
}
}
类程序
{
静态字典_dict=新字典();
静态void Main(字符串[]参数)
{
_添加(“{Jwala Vora#3/13}”,“someValue1”);
_dict.Add(“{Amazon Vally#2/11}”,“someValue2”);
_dict.Add(“{1#3/75}”,“someValue3”);
_dict.Add(“{MdOffice employee#1/1}”,“someValue4”);
var input=@“由{MdOffice employee#1/1}向{Jwala Vora#3/13}提交{Amazon Vally#2/11}{1#3/75}的提案”;
var result=Regex.Replace(输入@“{.*.}”,求值);
控制台写入线(结果);
}
私有静态字符串求值(匹配)
{
返回_dict[match.Value];
}
}
你能举一个例子说明你将如何重新计算{}的内容吗?我正在寻找你的解决方案。但它不起作用。正则表达式有问题。@Priyanka:很抱歉,我刚刚更正了正则表达式。
class Program
{
static Dictionary<string, string> _dict = new Dictionary<string, string>();
static void Main(string[] args)
{
_dict.Add("{Jwala Vora#3/13}","someValue1");
_dict.Add("{Amazon Vally#2/11}", "someValue2");
_dict.Add("{1#3/75}", "someValue3");
_dict.Add("{MdOffice employee#1/1}", "someValue4");
var input = @"Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}";
var result = Regex.Replace(input, @"{.*?}", Evaluate);
Console.WriteLine(result);
}
private static string Evaluate(Match match)
{
return _dict[match.Value];
}
}