2011-10-18 1 views
6

में पथ से मिलान करने के लिए रेगेक्स नियमित अभिव्यक्तियों के साथ नया हूं। मैं निम्नलिखित लाइनों से पथ को निकालने के लिए की जरूरत है:सी #

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

मैं एक विधि है कि किसी भी लाइन के रास्ते लौट लागू करना होगा। पहला स्तंभ 1 या अधिक वर्ण वाले शब्द है, कभी खाली नहीं होता है, दूसरा स्तंभ पथ है। विभाजक 1 या अधिक रिक्त स्थान, या एक या अधिक टैब, या दोनों हो सकता है। (। यह मानते हुए किया जाता है कि पहले कॉलम कभी नहीं रिक्त स्थान या टैब शामिल हैं)

+0

इनपुट इनपुट या फ़ाइल अलग-अलग है? –

+0

@ रॉयनामिर क्या इससे कोई फर्क पड़ता है? – username

+0

हां। लाइन के लिए और फ़ाइल के लिए उपचार अलग है। जब तक आप इसे टेक्स फ़ाइल से लाइन से नहीं पढ़ते हैं और फिर आपको लाइन ब्रेक चेर्स इत्यादि की देखभाल करने की भी आवश्यकता होगी –

उत्तर

7

यह मेरे लिए लगता है कि आप सिर्फ

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

चाहते

संपादित करें: एक नियमित अभिव्यक्ति के रूप में आप शायद सिर्फ कर सकते हैं:

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

@ जोन: क्षमा करें, मुझे नियमित रूप से विस्तार की आवश्यकता है क्योंकि मैं .NET 1.1 का उपयोग कर रहा हूं और मेरे पास StringSplitOptions.RemoveEmptyEntries अधिभार तक कोई पहुंच नहीं है। फिर भी धन्यवाद! –

+0

@ डैनियल पिनलाबा: यह शुरू करने के लिए कहने के लिए उपयोगी होता - आवश्यकता है कि .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

@ जोन्स स्केट मैं सहमत हूं। विंडोज़ पथ के लिए यह एक सामान्य सामान्य regex है। – FailedDev

+0

@FailedDev यह उदाहरण के लिए "k: \ test \ test" के लिए काम नहीं करता है। अगर मैं ** \\ test \ t><* st ** जैसे पथ को पारित करने का प्रयास करता हूं तो यह मान्य होगा। मुझे यह रेगेक्स '^ (?: [सी-जेडसी-जेड] \: | \\) मिला है (\\ [ए-जेए-जेड _ \ - \ s0-9 \।] +) +'। यह मेरी राय पर सही ढंग से पथ को मान्य करता है। इसे मिला [यहां] (https://www.codeproject.com/Tips/216238/ नियमित- अभिव्यक्ति-to- वैध- फ़ाइल- पैथ- और- EXxten) – Potato

0

Regex Tester Regex तेजी से परीक्षण करने के लिए एक अच्छा वेबसाइट है।

Regex.Matches(input, "([a-zA-Z]*:[\\[a-zA-Z0-9 .]*]*)");