简体   繁体   English

C#Regex表达式从文件路径获取某些单词

[英]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. 大家好,我正在编写一个ac#应用程序,我似乎无法使用regex作为id来拆分数据。

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. 从那里,我将各个字符串存储到3个数组中。 Problem is, within the loop the data will change so you cant use Contains("ABE1") 问题是,在循环内数据将更改,因此您不能使用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() : 然后使用SubString()获得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: 使用常规模式“ ABC1_xy_YYYY”时,最好使用下划线作为定界符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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