本文介绍了如何从名称中搜索数字并将其放入文件路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个项目,它要求我从名称中搜索一个数字并将其放入我的文件路径中。例如:



名称为LU610364G02



,文件路径应为:.. \ Report \ \\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 。



如何搜索值并将其放入文件路径?

我使用的是Microsoft Visual Studio,c#。

提前感谢您的帮助



我尝试过:



尝试使用Regex作为表达式,将名称与文件路径匹配。



Hi. I have a project and it requires me to search a number from a name and put it into my filepath. For example:

The name is LU610364G02

and the filepath should be: ..\Reports\2016\10\LU610364\

where 2016 is the 3rd number from LU610364G02, 10 is the 4th and 5th number and LU610364 is the first 8 numbers.

How do I search the values and put it into the filepath?
I am using Microsoft Visual Studio, c#.
Thank you in advance for the help

What I have tried:

Tried using Regex as an expression to match the name to the filepath.

<pre lang="c#"> string Timestamp = string.Empty;
            using (var file = System.IO.File.OpenText(datFile))
            {
                //regexs = regular expressions
                //compile regexs
                //Regex nodeRegex = new Regex("NODE      :(.*)");
                //Regex testModeRegex = new Regex("TEST MODE:(.*)");
                //Regex timestampRegex = new Regex("Timestamp:(.*)");

                Regex nodeRegex = new Regex("NODE      :(.*)", RegexOptions.IgnoreCase);
                Regex testModeRegex = new Regex("\bTest Mode\b", RegexOptions.IgnoreCase);
                Regex program = new Regex("\bPROGRAM\b", RegexOptions.IgnoreCase);
                Regex TimeStamp = new Regex("\bTIMESTAMP\b", RegexOptions.IgnoreCase);

                while (!file.EndOfStream)
                {
                    String line = file.ReadLine();

                    // check name
                    Match m = nodeRegex.Match(line);


                    //string line = "TUI Test Mode: B31P";
                    string match = string.Format(@"\b{0}\b", FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode);
                    bool matchTestMode = Regex.IsMatch(line, match, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    Match matchTest = Regex.Match(line, FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode);
                    Match matchProgram = Regex.Match(line, FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexPROGRAM);
                    Match matchTimestamp = Regex.Match(line, FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTimestamp);


                    if (matchTest.Success)
                    {
                        int startIndex = matchTest.Index + FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode.Length + 1;
                        int endIndex = line.IndexOf('' '', startIndex + 1);

                        TestMode = line.Substring(startIndex, endIndex - startIndex).Trim();
                        //break;
                    }

                    if (matchProgram.Success)
                    {
                        int startIndex = matchProgram.Index + FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode.Length + 1;
                        int endIndex = line.IndexOf('' '', startIndex + 1);

                        strProgram = line.Substring(startIndex, endIndex - startIndex).Trim();
                        //break;
                    }

                    if (matchTimestamp.Success)
                    {
                        int startIndex = matchTimestamp.Index + FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTimestamp.Length + 1;
                        int endIndex = line.IndexOf('' '', startIndex + 1);

                        strTimestamp = line.Substring(startIndex, endIndex - startIndex).Trim();
                        //break;

推荐答案

string nam = "LU610364G02" ;
string pth = System.Text.RegularExpressions.Regex.Replace ( nam , @"^(..(.)(..)...).*




这篇关于如何从名称中搜索数字并将其放入文件路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:03