简体   繁体   中英

C#Regex Expression Getting certain words from a file path

Hey guys im writing ac# application and i cant seem to split up data using regex as id like to.

C:\Users\e014425c\Downloads\StoreData\ABE1102013.csv

Thats getting stored as a string within the loop. I want:-

 ABE1
 10
 2013

From there i would store the individual strings into 3 arrays. Problem is, within the loop the data will change so you cant use Contains("ABE1")

Help would be much appreciated!

foreach (string word in file)
{

    string testString = word;
    string firstWords = Regex.Match(testString, @"^(\w+\b.*?){6}").ToString();

    Console.Write(firstWords);
}

Thats how i´ve been testing the data

First get the filename only from the path:

string filename = File.FilenameWithoutExtension(path);

Then the characters you want with SubString() :

string a1 = filename.SubString(0, 4);
string a2 = filename.SubString(4, 2);
string a3 = filename.SubString(6, 4);


EDIT

With a general pattern "ABC1_xy_YYYY" it is better to use the underscores as delimiters:

 string[] field = filename.Split('_');
 a1 = field[0];
 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM