Regex以匹配C#中的路径

本文关键字:路径 Regex | 更新日期: 2023-09-27 18:11:38

我是正则表达式的新手。我需要从以下行提取路径:

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

我需要实现一个返回给定行的路径的方法。第一列是包含1个或多个字符的单词,从不为空,第二列是路径。分隔符可以是1个或多个空格,也可以是一个或多个子选项卡,或者两者兼有。

Regex以匹配C#中的路径

听起来你只是想要

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;
    }    
}
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)
)
"

Regex Tester是一个快速测试Regex的好网站。

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