2011-10-18 6 views
6

正規表現は新しくなっています。C#でパスにマッチする正規表現

XXXX  c:\mypath1\test 
YYYYYYY    c:\this is other path\longer 
ZZ  c:\mypath3\file.txt 

与えられた行のパスを返すメソッドを実装する必要があります。次の行からパスを抽出する必要があります。最初の列は1文字以上の単語で、決して空ではない、2番目の列はパスです。区切り記号は、1つ以上のスペース、1つ以上のタブ、またはその両方にすることができます。

+0

は入力ファイルですか? –

+0

@RoyiNamirは問題ですか? – username

+0

はい。行とファイルの処理が異なります。あなたがtexファイルから行ごとにそれを読んでから、改行文字などを扱う必要がある場合は除きます。 –

答えて

7

あなただけ

string[] bits = line.Split(new char[] { '\t', ' ' }, 2, 
          StringSplitOptions.RemoveEmptyEntries); 
// TODO: Check that bits really has two entries 
string path = bits[1]; 

をしたいようですが(これが最初の列がスペースまたはタブが含まれていないことを想定している。)

EDIT私に聞こえる:あなたはおそらくちょうど行うことができます正規表現として:

Regex regex = new Regex(@"^[^ \t]+[ \t]+(.*)$"); 

サンプルコード:

using System; 
using System.Text.RegularExpressions; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     string[] lines = 
     { 
      @"XXXX  c:\mypath1\test", 
      @"YYYYYYY    c:\this is other path\longer", 
      @"ZZ  c:\mypath3\file.txt" 
     }; 

     foreach (string line in lines) 
     { 
      Console.WriteLine(ExtractPathFromLine(line)); 
     } 
    } 

    static readonly Regex PathRegex = new Regex(@"^[^ \t]+[ \t]+(.*)$"); 

    static string ExtractPathFromLine(string line) 
    { 
     Match match = PathRegex.Match(line); 
     if (!match.Success) 
     { 
      throw new ArgumentException("Invalid line"); 
     } 
     return match.Groups[1].Value; 
    }  
} 
+0

パスには空白が含まれている可能性があります。 – xanatos

+0

@Jon:申し訳ありませんが、私は.NET 1.1を使用しているので、定期的な表現が必要です。また、StringSplitOptions.RemoveEmptyEntriesオーバーロードへのアクセス権がありません。とにかくありがとう! –

+0

@DanielPeñalba:これから始めるには、そういうことを言っておくと便利でしょう - 最近、.NET 1.1は非常にまれです。編集します。 –

4
StringCollection resultList = new StringCollection(); 
try { 
    Regex regexObj = new Regex(@"(([a-z]:|\\\\[a-z0-9_.$]+\\[a-z0-9_.$]+)?(\\?(?:[^\\/:*?""<>|\r\n]+\\)+)[^\\/:*?""<>|\r\n]+)"); 
    Match matchResult = regexObj.Match(subjectString); 
    while (matchResult.Success) { 
     resultList.Add(matchResult.Groups[1].Value); 
     matchResult = matchResult.NextMatch(); 
    } 
} catch (ArgumentException ex) { 
    // Syntax error in the regular expression 
} 

内訳:

@" 
(       # Match the regular expression below and capture its match into backreference number 1 
    (       # Match the regular expression below and capture its match into backreference number 2 
     |        # Match either the regular expression below (attempting the next alternative only if this one fails) 
     [a-z]       # Match a single character in the range between “a” and “z” 
     :        # Match the character “:” literally 
     |        # Or match regular expression number 2 below (the entire group fails if this one fails to match) 
     \\       # Match the character “\” literally 
     \\       # Match the character “\” literally 
     [a-z0-9_.$]     # Match a single character present in the list below 
              # A character in the range between “a” and “z” 
              # A character in the range between “0” and “9” 
              # One of the characters “_.$” 
      +        # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
     \\       # Match the character “\” literally 
     [a-z0-9_.$]     # Match a single character present in the list below 
              # A character in the range between “a” and “z” 
              # A character in the range between “0” and “9” 
              # One of the characters “_.$” 
      +        # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
    )?       # Between zero and one times, as many times as possible, giving back as needed (greedy) 
    (       # Match the regular expression below and capture its match into backreference number 3 
     \\       # Match the character “\” literally 
     ?        # Between zero and one times, as many times as possible, giving back as needed (greedy) 
     (?:       # Match the regular expression below 
     [^\\/:*?""<>|\r\n]    # Match a single character NOT present in the list below 
              # A \ character 
              # One of the characters “/:*?""<>|” 
              # A carriage return character 
              # A line feed character 
      +        # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
     \\       # Match the character “\” literally 
    )+       # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
    ) 
    [^\\/:*?""<>|\r\n]    # Match a single character NOT present in the list below 
            # A \ character 
            # One of the characters “/:*?""<>|” 
            # A carriage return character 
            # A line feed character 
     +        # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
) 
" 
+1

これは、基本的に最初の空白/タブのセットの後にすべてを取得するのは非常に複雑です。 –

+0

@JonSkeet私は同意します。これは、Windowsパスのより一般的な正規表現です。 – FailedDev

+0

@FailedDev "k:\ test \ test"のように動作しません。 ** \\ test \ t><* st **のようなパスを渡そうとすると有効になります。私はこの正規表現 '^(?:[c-zC-Z] \:| \\)(\\ [a-zA-Z _ \ - \ s0-9 \。] +)+'を見出しました。それは私の意見で正しくパスを検証します。見つけた[ここ](https://www.codeproject.com/Tips/216238/Regular-Expression-to-Validate-File-Path-and-Exten) – Potato

0

Regex Testerは、正規表現は、高速テストする良いウェブサイトです。

Regex.Matches(input, "([a-zA-Z]*:[\\[a-zA-Z0-9 .]*]*)"); 
関連する問題