【问题标题】:Substitution of comma separated values替换逗号分隔值
【发布时间】:2013-10-18 14:25:21
【问题描述】:

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

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

感谢您的帮助。

【问题讨论】:

  • 当然可以,但可能不会为您带来任何显着的性能提升,并且可能会让下一个遇到它的人感到困惑。
  • Regex 是适合这项工作的错误工具。如果您知道您的数据总是像您在问题中显示的那样简单,那么请执行简单的拆分(“,”)。否则,请考虑使用 CSV 库,例如 codeproject.com/Articles/9258/A-Fast-CSV-Reader,甚至是内置的 Oledb 提供程序。

标签: c# regex


【解决方案1】:

没有显式循环(虽然没有正则表达式)...

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>

【讨论】:

    【解决方案2】:

    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>
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

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

      【讨论】:

        【解决方案4】:

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

        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>
        

        【讨论】:

          【解决方案5】:

          此替换通过...处理逗号(如果存在)将它们留在其中。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多