2015-11-08 31 views
-2

從文本文件中的所有的話我有一個文本文件,它看起來像這樣:C#獲取開始與X

( 'STEAM_1:0:XXXXXXXX', '用戶1', '德國',73677,0, 0,105,151,105,'2015-09-05'), ('STEAM_1:0:XXXXXX','User2','Belgium',64005,3,100,161,277,161,'2015- ('STEAM_1:0:XXXXXXX','User3','丹麥',52226,0,0,84,146,84,'2015-11-05'), ('STEAM_1: 0:XXXXXXXXX','User4','丹麥',48300,0,0,98,291,98,'2015-09-29'), ('STEAM_1:0:XXXXXXXX','User5','Denmark ',47817,4,1000,104,272,104,'2015-11-08'),

我該如何提取「STEAM_1:0:XXXXXXX」,而沒有其他內容才能提供給整個新文件?我有250個用戶。

我對編程非常陌生,但我嘗試了一些東西,但沒有任何工作。 任何意見將不勝感激。

+3

請發佈一個你已經嘗試過的代碼的例子,以便我們可以更好地協助。 – TheInnerLight

+1

使用'string.Split'方法,首先由'Environment.NewLine'拆分,然後用''''拆分結果數組中的每個標記,'' - 爲每個拆分標記獲取第一個子標記並刪除前兩個字母,有它。另一種選擇是使用正則表達式,我不是一個粉絲,所以別人可以給你一個想法,祝​​你好運;) – kirotab

回答

1

使用正則表達式

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.txt"; 
     static void Main(string[] args) 
     { 
      string input = File.ReadAllText(FILENAME); 
      string pattern = @"\('(?'steam'[^']*)"; 
      MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Singleline); 
      foreach (Match match in matches) 
      { 
       Console.WriteLine(match.Groups["steam"]); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 
​ 
+0

謝謝!我在foreach之前添加了StreamWriter,它完美地實現了我想要實現的目標。 – ErikJ

0

我會做這樣的

List<string> formattedList = new List<string>(); 

     //read from .txt file 
     using (StreamReader sr = new StreamReader("Source path.txt")) 
     { 
      string line = sr.ReadLine(); 

      //get index of second occurance of the ' char. 
      int length = line.IndexOf("'", 2); 

      //start 2 indexes in and return the substring until second 
      //occurance of ' char 
      string formattedLine = line.Substring(2, length); 
      formattedList.Add(formattedLine); 

     } 

     //write to .txt file 
     using (StreamWriter sw = new StreamWriter("Destination Path.txt")) 
     { 
      foreach (var line in formattedList) 
      { 
       sw.WriteLine(line); 
      } 
     } 

東西欲瞭解更多信息閱讀 https://msdn.microsoft.com/en-us/library/8bh11f1k.aspxhttps://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx

0

心不是最優雅的解決方案,但它的工程!

static void Main(string[] args) 
    { 
     var text = 
@"('STEAM_1:0:XXXXXXXX', 'User1', 'Germany', 73677, 0, 0, 105, 151, 105, '2015-09-05'), 
('STEAM_1:0:XXXXXX', 'User2', 'Belgium', 64005, 3, 100, 161, 277, 161, '2015-11-08'), 
('STEAM_1:0:XXXXXXX', 'User3', 'Denmark', 52226, 0, 0, 84, 146, 84, '2015-11-05'), 
('STEAM_1:0:XXXXXXXXX', 'User4', 'Denmark', 48300, 0, 0, 98, 291, 98, '2015-09-29'), 
('STEAM_1:0:XXXXXXXX', 'User5', 'Denmark', 47817, 4, 1000, 104, 272, 104, '2015-11-08'),"; 

     var split = text.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries); 

     var sb = new StringBuilder(); 
     foreach (string line in split) 
     { 
      var firstCommer = line.IndexOf(","); 

      sb.AppendLine(line.Substring(2, firstCommer - 3)); 
     } 


     File.WriteAllText(@"c:\temp\out.txt", sb.ToString()); 
    }