2016-12-19 3 views
2

私は解析しているもののタイトル行が多少複雑であるため、私はデータ解析で不具合を起こしました。年、タイトル、エディションはありますが、必ずしもそうではありません。年と版はintに変換できますが、残りはできません。私はそれが毎回どこにあるのか分からなければ、どのようにして年を分割することができますか?テキストの行からどのようにintを抽出できますか?

のデータセットの例:

2016スーパー特別レギュラーシーズン、01第十五tossup

数学の問題は、ラウンド02 2013豊富な問題

FooBarの素晴らしいゲームパート1 0F 2ラウンド03の問題2015

を監督

複数の数字があるため、文字が数字であるかどうかを調べるだけでは、行全体をテストすることはできません。あらかじめ日付を知らないためIndexOfのようなことをすることはできません。

答えて

5

文字列からすべての数字は、正規表現から結果値を取得する

 /* \d+ Is used to find integers */ 
    Regex regex = new Regex(@"\d+"); 

    // Loop thrue all matches 
    foreach (Match match in regex.Matches("2016 Super special regular season, 01 fifteenth tossup")) 
    { 
     Console.WriteLine(match.Value); /* Test output */ 
     int i = Convert.ToInt32(match.Value); /* Convert To Int and do something with it */ 
    } 

     ============ output =========== 
     2016 
     01 


    /* Use this \d{4} to return the 4 character from current match from \d*/ 
    /* (Example) => 12564568 => (output) : 1256 and 4568 */ 
    /* (Notice!!) If you use \d{4} and there are only 2 numbers found by \d 
     It has no result. */ 

または1つのラインでから すべての一致を得るためにregex.Matches()メソッドを使用して取得するには最初の発生数:

string resultString = Regex.Match(subjectString /*string to test */, @"\d+").Value; 
+1

これはタイトルの質問に回答しますが、問題の本文を無視しているようです。 – Abion47

+0

私はそれを更新しました。一番上のものは彼が尋ねたことを正確に説明しています。文字列からすべての単一の数字を取得し、それを整数に変換します。 –

+0

これはとても良い説明です。これに関しては、正規表現の改善のみが可能です。 '\ d {4}'。これは、4文字の整数だけを返します。 –

1
var line = "FooBar the amazing game part 1 0f 2 round 03 problems 2015"; 

var numbers = line.Split(' ').Where(word => word.All(char.IsDigit)).Select(int.Parse).ToList(); 

ここでは、1,2,3、および2015の整数があります。

年はあなた次第です。おそらく1900年から2017年の間にあるかどうかを確認してください。

3

使用正規表現:

string pattern_Year = @"\(\d{4}\)"; 
    string pattern_Edition = @"\(\d{2}\)"; 
    string search = "2016 Super special regular season, 01 fifteenth tossup"; 
    var year = Regex.Matches(search, pattern_Year); 
    var edition = Regex.Matches(search, pattern_Edition); 
    if(year.Count > 0) 
     Console.WriteLine(year[0].Value); 
    if(edition.Count > 0) 
     Console.WriteLine(edition [0].Value); 
+0

あなたの正規表現パターンは何を説明できますか?私は実際にそれを頻繁に使用しないで、パターン内のシンボルは何を覚えていない – Alexandre

+0

文字列pattern_Year = @ "\(\ d {4} \)"、それはシンプルで数字が年として4桁を持つことがわかります。 –

1

このような何か:

static int GetYearFromTextLine(string s) 
    { 
     string [] words = s.Split(' '); 

     foreach (string w in words) 
     { 
      int number = 0; 
      if (int.TryParse(w, out number)) 
      { 
       // assume the first number found over "1900" must be a year 
       // you can modify this test yourself 
       if (number >= 1900) 
       { 
        return number; 
       } 
      } 
     } 
     return 0; 
    } 
    static void Main(string[] args) 
    { 
     Console.WriteLine(GetYearFromTextLine("Math problems galore 2013 Round 02 directed problems")); 
    } 
1

このスクリプトの

string strValue = "abc123def456"; 
    char[] charArr = strValue.ToCharrArray(); 
    List<int> intList = new List<int>(); 
    for(int i =0; i < charArr.Length; i++) 
     { 
     string tmpInt =""; 
     if(char.IsDigit(charArr[i])) 
      { 
      tmpInt += charArr[i]; 
      while((i < charArr.Lenght -1) && char.IsDigit([i + 1) 
       { 
        tmpInt += charArr[i+1]; 
        i++; 
       } 
      } 
      if(tmpInt != "") 
      intList.Add(int.Parse(tmpInt)); 
     } 

利点は桁がにある場所は関係ありません、で動作するはずです、これを試してみてください文字列は分割または任意のパターンに依存しません。

関連する問題