2016-01-18 158 views
3

我正在閱讀文本文件行按行和我想要在檢查包含特殊字符的行是否包含特殊字符之後獲取數據。在我的情況下,我想檢查行是否包含<#Tag()>以及if然後它包含讀取)之間的串(即線具有<#Tag(param1)>那麼它應該返回param1從文件中獲取文本C#

但問題是線可以包含超過一個<#Tag()> 對於實施例線路是 - <#Tag(value1)> <#Tag(value2)> <#Tag(value3)> 然後,它應該返回第一value1然後value2然後value3

string contents = File.ReadAllText(@"D:\Report Format.txt"); 
int start = contents.IndexOf("Header") + "Header".Length; 
int end = contents.IndexOf("Data") - "Header".Length; 
int length = end - start; 
string headerData = contents.Substring(start, length); 
headerData = headerData.Trim(' ', '-'); 
MessageBox.Show(headerData); 
using (StringReader reader = new StringReader(headerData)) 
{ 
    string line; 
    while ((line = reader.ReadLine()) != null) 
    { 
     if (line.Contains("<#Tag")) 
     { 
      string input = line; 
      string output = input.Split('<', '>')[1]; 
      MessageBox.Show(output); 
      Globals.Tags.SystemTagDateTime.Read(); 
      string newoutput = Globals.Tags.SystemTagDateTime.Value.ToString(); 
      input = input.Replace(output, newoutput); 
      input = Regex.Replace(input, "<", ""); 
      input = Regex.Replace(input, ">", ""); 
      MessageBox.Show(input); 
     } 
    } 
} 
+0

你試過使用正則表達式嗎? – tchelidze

+2

請耐心等待正則表達式大師出現在一行中。 –

+0

如果它可能比這更復雜,你可能會看着一個解析器,而不是試圖通過字符串操作或正則表達式來做到這一點。 –

回答

0

你可以用正則表達式(我將在一個工作)做到這一點 - 但作爲一個簡單的快捷方式只是做:

 
var tags = line.Split(new string[] { "<#Tag" }, StringSplitOptions.None); 
foreach(var tag in tags) 
{ 
//now parse each one 
} 

我看到tchelidze剛剛張貼的正則表達式,看起來還不錯,所以我」我會按照正則表達式來回答這個問題。

+0

無法將字符串轉換爲char []錯誤。 –

+0

Doh,請參閱編輯。多字符語法是不同的。 –

+0

它的作品,但我也想要它的價值取代它,並將其放在一個字符串具有相同的空間,因爲它是例如<#標籤(value1)><#標籤(value2)><#標籤(value3)>然後value1 value2 value3(與<#Tag(value1)><#Tag(value2)><#Tag(value3)>)相同的空間,然後是它的值10 20 30(與<#Tag(value1)><#相同的空間) Tag(value2)><#Tag(value3)>) –

3

嘗試以下

var matches = Regex.Matches(line, @"(?<=\<\#Tag\()\w+(?=\)\>)") 
foreach (Match match in matches) 
    MessageBox.Show(match.Value); 

如果你想完成在註釋中描述情況下嘗試以下。

var line = "<#Tag(value1)> <#Tag(value2)> <#Tag(value3)>"; 
    var matches = Regex.Matches(line, @"(?<=\<\#Tag\()\w+(?=\)\>)"); 
    //use matches in your case to find values. i assume 10, 20 , 30 
    var values = new Dictionary<string, int>() { { "value1", 10 }, { "value2", 20 }, { "value3", 30 } }; 
    const string fullMatchRegexTemplate = @"\<\#Tag\({0}\)\>"; 
    foreach (var value in values) 
    Regex.Replace(line, string.Format(fullMatchRegexTemplate, value.Key), value.Value.ToString()); 
+0

這似乎讓你開始,但它似乎只匹配在線正則表達式測試中的第一個:https://regex101.com/ –

+0

@AdamHouldsworth請添加全局修改。 – tchelidze

+1

那麼你會看看,太棒了。 –

0

[^a-zA-Z0-9] 

基本上所有非字母數字字符匹配這可能做的伎倆。

private void removeTag() 
    { 
     string n = "<#Tag(value1)> <#Tag(value2)> <#Tag(value3)>"; 
     string tmp = Regex.Replace(n, "Tag+", ""); 
     tmp = Regex.Replace(tmp, "[^0-9a-zA-Z]+", ",") ; 
    } 

另外一個可能是

string tmp = Regex.Replace(n, "[^0-9a-zA-Z]*[Tag]*[^0-9a-zA-Z]", ","); 
0

您還可以通過不斷的價值<#Tag()>這樣分割後的字符串收集他們:

string str = "<#Tag(value1)> <#Tag(value2)> <#Tag(value3)>"; 
string[] values = str.Split(new string[] { "<#Tag(", ")>" }, StringSplitOptions.RemoveEmptyEntries); 

值包含:

value1, value2, value3 

顯示在MessageBox的結果:

foreach (string val in values) { 
    if (!(String.IsNullOrEmpty(val.Trim()))) { 
     MessageBox.Show(val); 
    } 
} 

編輯根據您發表評論:

我可以在一個消息框中顯示完整的值1值2值3不是逗號,但具有相同的間距,因爲它是

string text = ""; 
foreach (string val in values) { 
    text += val; 
} 
MessageBox.Show(text); 

基於評論: 現在最後一個查詢在顯示在消息框之前,我想用它的值替換它,例如10 20 a nd 30

string text = ""; 
foreach (string val in values) { 
    // where val is matching your variable (let's assume you are using dictionary for storing the values) 
    // else is white space or other... just add to text var. 
    if (yourDictionary.ContainsKey(val)) { 
     text += yourDictionary[val]; 
    } else { 
     text += val; 
    } 
} 
MessageBox.Show(text); 
+0

我想爲value1,value2,value3彈出3個單獨的消息框。它是動態的,它可能是4或5或1 –

+0

我添加了用於在MessageBox中顯示結果的代碼。 這將適用於您的字符串中的1,3,99 ...匹配。 – DDan

+0

現在它也處理空白。 – DDan