Regex to match a path in C#

Daniel Peñalba picture Daniel Peñalba · Oct 18, 2011 · Viewed 13.2k times · Source

I'm new with regular expressions. I need to extract the path from the following lines:

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

I need to implement a method that return the path of a given line. The first column is a word with 1 or more characters, never is empty, the second column is the path. The separator could be 1 or more spaces, or one or more tabs, or both.

Answer

Jon Skeet picture Jon Skeet · Oct 18, 2011

It sounds to me like you just want

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

(This is assuming that the first column never contains spaces or tabs.)

EDIT: As a regular expression you can probably just do:

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

Sample code:

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