2013-10-17 206 views
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> 

結果數組更容易,但在我的特殊情況下,我想知道是否可以生成新的字符串而不必分割和循環每個列表位置。

謝謝你的幫助。

+8

當然可能,但可能不會給你帶來任何顯着的性能提升,它可能會讓下一個碰到它的人感到困惑。 – Ocelot20

+0

正則表達式是這個工作的錯誤工具。如果你知道你的數據總是和你在問題中顯示的一樣簡單,那麼做一個簡單的分割(「,」)。否則,請考慮使用CSV庫,例如http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader或甚至內置的Oledb提供程序。 –

回答

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

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

*/