2014-10-09 214 views
0

我有一個txt文件中提取字符串,它包含了一些,我想提取值:像數後"YOUR NUMBER:"要從一個txt文件

"YOUR NUMBER:"已經沒有空間,由第一組3位數由「 - 」和#digits

---***--- 
i.e.: 
[some text here] 

YOUR NUMBER: 
123-12345678 

[some other text here] 
---***--- 

我開始看RegEx,但說實話,我在編碼技術差,所以我尋求幫助。 我需要提取並將其保存在Excel,SQL或類似文件中。

任何幫助?

回答

0

嘗試RegExs這裏有一個非正則表達式apporach:

string[] lines = File.ReadAllLines(path); 
List<string> numbers = new List<string>(); 
for (int i = 0; i < lines.Length - 1; i++) 
{ 
    string line = lines[i]; 
    if (line.Trim().Equals("YOUR NUMBER:", StringComparison.InvariantCultureIgnoreCase)) 
    { 
     string number = lines[i + 1].Trim(); 
     string[] bothNums = number.Split('-'); 
     if (bothNums.Length == 2 && bothNums[0].Length == 3 && bothNums.All(n => n.All(Char.IsDigit))) 
      numbers.Add(number); 
    } 
}