替换逗号分隔值

时间:2013-10-17 20:09:29

标签: 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='details.aspx?location=CHA'>CHA</a>, <a href='details.aspx?location=INDY'>INDY</a>

我知道使用split(“,”)然后循环遍历生成的数组会更容易,但在我的特定情况下,我想知道是否可以生成新的字符串而不必拆分和循环每个单一的名单位置。

感谢您的帮助。

5 个答案:

答案 0 :(得分:2)

没有明确的循环(虽然没有正则表达式)......

var list = "AUSTIN,ORL2,ORL6";
Console.WriteLine(string.Join(",",list.Split(',').Select(x=> "<a href='details.aspx?location="+x+"'>"+x+"</a>").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>

答案 1 :(得分:1)

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);
        }
    }
}

输出结果为:

<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>

答案 2 :(得分:0)

您可以执行以下操作:

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 = "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);

答案 3 :(得分:0)

这是另一种方法。正则表达式是如此多才多艺。

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));
}

请注意,我完全按照您的要求提出您的要求。因此,输出正是您所要求的:

<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>

答案 4 :(得分:0)

这个替换处理逗号...如果存在则将它们留在其中。

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>

*/