C# 逗号分隔值的替换

C# 逗号分隔值的替换,c#,regex,C#,Regex,我有一个字符串列表,每个位置都包含逗号分隔的值,如下所示: AUSTIN,ORL2,ORL6 CHA,INDY <a href='details.aspx?location=AUSTIN'>AUSTIN</a>, <a href='details.aspx?location=ORL2'>ORL2</a>, <a href='details.aspx?location=ORL6'>ORL6</a> <a href='d

我有一个字符串列表,每个位置都包含逗号分隔的值,如下所示:

AUSTIN,ORL2,ORL6
CHA,INDY
<a href='details.aspx?location=AUSTIN'>AUSTIN</a>, <a href='details.aspx?location=ORL2'>ORL2</a>, <a href='details.aspx?location=ORL6'>ORL6</a>
<a href='details.aspx?location=CHA'>CHA</a>, <a href='details.aspx?location=INDY'>INDY</a>
等等。是否有任何方法可以使用正则表达式匹配值并替换/重用匹配的值以生成新字符串,如下所示:

AUSTIN,ORL2,ORL6
CHA,INDY
<a href='details.aspx?location=AUSTIN'>AUSTIN</a>, <a href='details.aspx?location=ORL2'>ORL2</a>, <a href='details.aspx?location=ORL6'>ORL6</a>
<a href='details.aspx?location=CHA'>CHA</a>, <a href='details.aspx?location=INDY'>INDY</a>
,
, 
我知道使用split(“,”)然后在结果数组中循环要容易得多,但在我的特殊情况下,我想知道是否可以只生成新字符串而不必拆分和循环每个列表位置

谢谢您的帮助。

没有显式循环(尽管没有正则表达式)

var list=“奥斯汀,奥尔良2号,奥尔良6号”;
Console.WriteLine(string.Join(“,”,list.Split(“,”).Select(x=>”).ToArray());
//输出

<a href='details.aspx?location=AUSTIN'>AUSTIN</a>,<a href='details.aspx?location=ORL2'>ORL2</a>,<a href='details.aspx?location=ORL6'>ORL6</a>
,,
Ocelot20说出了一切

不过,这里有一个使用正则表达式的快速程序:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"\w+";
        Regex regex = new Regex(pattern);
        string sentence = "AUSTIN,ORL2,ORL6\nCHA,INDY";

        foreach (Match match in regex.Matches(sentence))
        {
            Console.WriteLine("<a href='details.aspx?location={0}'>{0}</a>",
                              match.Value);
        }
    }
}
使用系统;
使用System.Text.RegularExpressions;
公开课范例
{
公共静态void Main()
{
字符串模式=@“\w+”;
正则表达式正则表达式=新正则表达式(模式);
字符串语句=“奥斯汀,奥尔良2号,奥尔良6号\nCHA,印地”;
foreach(regex.Matches中的匹配(句子))
{
Console.WriteLine(“,
匹配值);
}
}
}
输出为:

<a href='details.aspx?location=AUSTIN'>AUSTIN</a>
<a href='details.aspx?location=ORL2'>ORL2</a>
<a href='details.aspx?location=ORL6'>ORL6</a>
<a href='details.aspx?location=CHA'>CHA</a>
<a href='details.aspx?location=INDY'>INDY</a>

您可以执行以下操作:

string input = "Austin,Dallas";
string format = "<a href='details.aspx?location=$1'>$1</a>\n";

var newStr = new Regex(@"(\w+),?").Replace(input, format);
string input=“奥斯汀,达拉斯”;
字符串格式=“\n”;
var newStr=新的正则表达式(@“(\w+),”)。替换(输入,格式);
但正如我之前所说的,无论是谁来看它,都会感到困惑(您还必须想出一个解决方案来删除可能的尾随换行符或逗号…)

编辑-以下是我实际使用的解决方案:

string input = "Austin,Dallas";
string format = "<a href='details.aspx?location={0}'>{0}</a>";

var formattedList = input.Split(',').Select (i => string.Format(format, i));
string result = string.Join(",", formattedList);
string input=“奥斯汀,达拉斯”;
字符串格式=”;
var formattedList=input.Split(',')。选择(i=>string.Format(Format,i));
字符串结果=string.Join(“,”,formattedList);

这里有另一种方法。正则表达式用途广泛

List<string> inputs = new List<string>() { "AUSTIN,ORL2,ORL6", "CHA,INDY" };
List<string> results = new List<string>();
foreach (string input in inputs)
{
    List<string> resultComponents = new List<string>();
    foreach (Match match in Regex.Matches(input, "([A-Z0-9]+(?=,)?)"))
    {
        if (match.Success) resultComponents.Add(string.Format("<a href='details.aspx?location={0}'>{0}</a>", match.Value));
    }
    results.Add(string.Join(", ", resultComponents));
}
List inputs=newlist(){“AUSTIN,ORL2,ORL6”,“CHA,INDY”};
列表结果=新列表();
foreach(输入中的字符串输入)
{
List resultComponents=新列表();
foreach(Regex.Matches中的匹配(输入,“([A-Z0-9]+(?=,))”)
{
if(match.Success)resultComponents.Add(string.Format(“,match.Value));
}
Add(string.Join(“,”,resultComponents));
}
请注意,我完全按照您的要求处理您的要求。因此,输出正是您所要求的:

<a href='details.aspx?location=AUSTIN'>AUSTIN</a>, <a href='details.aspx?location=ORL2'>ORL2</a>, <a href='details.aspx?location=ORL6'>ORL6</a>
<a href='details.aspx?location=CHA'>CHA</a>, <a href='details.aspx?location=INDY'>INDY</a>
,
, 

此替换通过……很好地保留逗号(如果存在)来处理逗号

var data = @"AUSTIN,ORL2,ORL6
CHA,INDY";

Console.WriteLine( Regex.Replace(data,
                                 "([^,\r\n]+)(,?)",
                                 "<a href='details.aspx?location=$1'>$1</a>$2"));

/* Output

<a href='details.aspx?location=AUSTIN'>AUSTIN</a>,<a href='details.aspx?location=ORL2'>ORL2</a>,<a href='details.aspx?location=ORL6'>ORL6</a>
<a href='details.aspx?location=CHA'>CHA</a>,<a href='details.aspx?location=INDY'>INDY</a>

*/
var data=@“奥斯汀,奥尔良2号,奥尔良6号
查印第”;
Console.WriteLine(正则表达式)替换(数据、,
“([^,\r\n]+)(,?)”,
"$2"));
/*输出
,,
,
*/

当然有可能,但可能不会给您带来任何显著的性能提升,而且可能会让下一个遇到它的人感到困惑。Regex是一个错误的工作工具。如果您知道您的数据总是像您在问题中显示的那样简单,那么请进行简单的拆分(“,”)。否则,考虑CSV库,甚至内置OLeDB提供程序。