2013-08-21 28 views
0

使用通配符搜索不同的字符串,例如搜索test0?(在?之後有一個空格)。搜索產生字符串:C#正則表達式通配符多個替換

​​3210

替換文本應是例如:

test0? - 

test0? -以上通配符表示1,2或3 ...

所以,替換字符串應該是:

test01 - 
test02 - 
test03 - 

string pattern = WildcardToRegex(originalText); 
fileName = Regex.Replace(originalText, pattern, replacementText); 

public string WildcardToRegex(string pattern) 
{ 
    return "^" + System.Text.RegularExpressions.Regex.Escape(pattern). 
     Replace("\\*", ".*").Replace("\\?", ".") + "$"; 
} 

問題是保存新字符串與原始字符加上添加的字符。我可以搜索字符串並用一些字符串操作保存原文,但這似乎是太多的開銷。必須有一個更簡單的方法。

感謝您的任何意見。

編輯:

使用通配符搜索字符串? 可能的字符串是:
TEST01 someText
test02 someotherText
TEST03 moreText

使用正則表達式,搜索字符串圖案將是:
TEST0? -
所以,每個字符串應該然後閱讀: '?'
TEST01 - someText
test02 - - someotherText
TEST03 moreText

如何保持這是通過正則表達式通配符代替字符

正如我的代碼所示,它會作爲測試出來嗎? - someText
這是錯誤的。

謝謝。

編輯民2

首先,感謝大家對他們的答案和方向。 它確實有幫助,並讓我走向正確的軌道,現在我可以更好地提出確切的問題:
它與替代有關。

在正則表達式後面插入文本。

我給出的示例字符串,它們可能並不總是處於該格式。我一直在考慮替代,但似乎無法得到正確的語法。我正在使用VS 2008.

還有更多建議嗎?

感謝

+2

您正在描述問題,好像我們都知道您正在處理的內容,並且已經進行了一段時間的項目。然而問題並不清楚。你能不能編輯你的問題,並用<20個字來描述你想要的內容?你可以放下一切,只是讓問題脫穎而出。 – Neolisk

+1

這是非常不明確和令人困惑的。從我所看到的,你想'test01'成爲'test01 - ',其中1是通配符。它看起來並不像我最終的字符串完全取決於通配符,爲什麼不把' - '加到字符串的末尾......? – tnw

+0

那麼'test0? '是一個定位器模板,'test0?-'是替代模板;那麼,根據什麼'?'匹配,它應該從定位器模板中取出並插入到替換模板中?是關於準確的? –

回答

2

如果您想更換 「TEST0?」 與 「TEST0 - ?」,你可以這樣寫:

string bar = Regex.Replace(foo, "^test0. ", "$0- "); 

這裏的關鍵是$0substitution,其中將包括匹配的文本。

所以,如果我正確地理解你的問題,你只是想你的replacementText"$0- "

+1

根據最新的編輯,你不想在你的表達式中追蹤'$',並且在你的替換字符串中需要額外的空間。 – dlras2

0

如果我正確地理解了這個問題,難道你只是使用一個匹配?

//Convert pattern to regex (I'm assuming this can be done with your "originalText") 
Regex regex = pattern; 

//For each match, replace the found pattern with the original value + " -" 
foreach (Match m in regex.Matches) 
{ 
    RegEx.Replace(pattern, m.Groups[0].Value + " -"); 
} 
0

所以我不是100%清楚你在做什麼,但我會試一試。

我打算假設您要使用「文件通配符」(?/*)並搜索一組匹配的值(同時保留使用佔位符本身存儲的值),然後將其替換爲新的價值(重新插入這些佔位符)。鑑於這一點,可能很多矯枉過正的(因爲你的要求是一種奇怪的)這裏就是我想出了:

// Helper function to turn the file search pattern in to a 
// regex pattern. 
private Regex BuildRegexFromPattern(String input) 
{ 
    String pattern = String.Concat(input.ToCharArray().Select(i => { 
     String c = i.ToString(); 
     return c == "?" ? "(.)" 
      : c == "*" ? "(.*)" 
      : c == " " ? "\\s" 
      : Regex.Escape(c); 
    })); 
    return new Regex(pattern); 
} 

// perform the actual replacement 
private IEnumerable<String> ReplaceUsingPattern(IEnumerable<String> items, String searchPattern, String replacementPattern) 
{ 
    Regex searchRe = BuildRegexFromPattern(searchPattern); 

    return items.Where(s => searchRe.IsMatch(s)).Select (s => { 
     Match match = searchRe.Match(s); 
     Int32 m = 1; 
     return String.Concat(replacementPattern.ToCharArray().Select(i => { 
      String c = i.ToString(); 
      if (m > match.Groups.Count) 
      { 
       throw new InvalidOperationException("Replacement placeholders exceeds locator placeholders."); 
      } 
      return c == "?" ? match.Groups[m++].Value 
       : c == "*" ? match.Groups[m++].Value 
       : c; 
     })); 
    }); 
} 

然後,在實踐中:

String[] samples = new String[]{ 
    "foo01", "foo02 ", "foo 03", 
    "bar0?", "bar0? ", "bar03 -", 
    "test01 ", "test02 ", "test03 " 
}; 

String searchTemplate = "test0? "; 
String replaceTemplate = "test0? -"; 

var results = ReplaceUsingPattern(samples, searchTemplate, replaceTemplate); 

其中,從samples列表以上,給我:

matched:  & modified to: 

test01   test01 - 
test02   test02 - 
test03   test03 - 

但是,如果你真的要保存你應該使用替代引用頭痛。沒有必要重新發明輪子。以上,與更換,可能已被更改爲:

Regex searchRe = new Regex("test0(.*)\s"); 
samples.Select(x => searchRe.Replace(s, "test0$1-")); 
0

你能趕上任何一件你匹配的字符串和任何地方的REPLACE語句,用符號$,然後擦肩而過元素的索引(它開始於索引1)。

你能趕上與括號元素「()」

例子:

如果我有幾個字符串與testXYZ,是XYZ一個3位數的號碼,我需要更換,比方說,與testZYX,反轉3位,我會做:

string result = Regex.Replace(source, "test([0-9])([0-9])([0-9])", "test$3$2$1"); 

所以,你的情況,這是可以做到:

string result = Regex.Replace(source, "test0([0-9]) ", "test0$1 - "); 
+1

有一個$ 0,它是整個匹配的字符串。$ 1是第一個捕獲組。 –