1

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

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(",") 然后循环遍历结果数组要容易得多,但在我的特殊情况下,我想知道是否可以只生成新字符串而不必拆分和循环每个列表位置。

谢谢您的帮助。

4

5 回答 5

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>
于 2013-10-17T20:29:00.633 回答
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>
于 2013-10-17T20:36:34.577 回答
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);
于 2013-10-17T20:49:35.977 回答
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>
于 2013-10-17T21:22:23.660 回答
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>

*/
于 2013-10-17T23:44:52.310 回答