使用正则表达式从路径中查找字符串

时间:2017-12-15 10:31:42

标签: c# regex

我有路径需要从中提取某些信息。

[\s\S]

我需要将SalesNo6提取到一个变量中,将Date20170101提取到另一个变量

string fullpath = @"D:\STACK\SalesNo6\Date20170101\Fileno16.txt";

但是我对正则表达式并不是很擅长,所以我无法理解如何获得理想的结果。有人可以建议我吗?感谢

2 个答案:

答案 0 :(得分:1)

以下是vaid模式

shlex
  • [root@stage conf]# protractor conf.js [10:17:16] I/launcher - Running 1 instances of WebDriver [10:17:16] I/hosted - Using the selenium server at http://localhost:4444/wd/hub [10:18:18] E/launcher - unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.34.522913 (36222509aa6e819815938cbf2709b4849735537c),platform=Linux 3.10.0-6 93.5.2.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 60.12 seconds Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z' System info: host: 'stage', ip: '10.10.19.203', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-693 .5.2.el7.x86_64', java.version: '1.8.0_151' Driver info: driver.version: unknown [10:18:18] E/launcher - WebDriverError: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.34.522913 (36222509aa6e819815938cbf2709b4849735537c),platform=Linux 3.10.0-6 93.5.2.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 60.12 seconds Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z' System info: host: 'stage', ip: '10.10.19.203', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-693 .5.2.el7.x86_64', java.version: '1.8.0_151' Driver info: driver.version: unknown at WebDriverError (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5) at Object.checkLegacyResponse (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/err or.js:546:15) at parseHttpResponse (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509: 13) at doSend.then.response (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:4 41:30) at process._tickCallback (internal/process/next_tick.js:103:7) From: Task: WebDriver.createSession() at Function.createSession (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriv er.js:769:24) at Function.createSession (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/chrome.js:7 61:15) at createDriver (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/index.js:170:33) at Builder.build (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/index.js:626:16) at Hosted.getNewDriver (/usr/lib/node_modules/protractor/built/driverProviders/driverProvider.js:53:33) at Runner.createBrowser (/usr/lib/node_modules/protractor/built/runner.js:195:43) at q.then.then (/usr/lib/node_modules/protractor/built/runner.js:339:29) at _fulfilled (/usr/lib/node_modules/protractor/node_modules/q/q.js:834:54) at self.promiseDispatch.done (/usr/lib/node_modules/protractor/node_modules/q/q.js:863:30) at Promise.promise.promiseDispatch (/usr/lib/node_modules/protractor/node_modules/q/q.js:796:13) [10:18:18] E/launcher - Process exited with error code 199 代表1位数字
  • string fullpath = @"D:\STACK\SalesNo6\Date20170101\Fileno16.txt"; string sales = Regex.Match(fullpath, @"SalesNo\d+").Value; string date = Regex.Match(fullpath, @"Date\d{8}").Value; 表示与日期匹配的8位数字

\d+代替RegEx

\d{8}

答案 1 :(得分:0)

这是解决问题的一种方法

string fullpath = @"D:\STACK\SalesNo6\Date20170101\Fileno16.txt";
string date="";
string salesNo="";
// provide the pattern: \d mean digit; + means 1 or more of that, () put the inner value into a group
Regex reg=new Regex(@".*SalesNo(\d+).*Date(\d+).*");
// check if its a "valid" input
if(reg.IsMatch(fullpath)){
    // do matching
    var matches=reg.Matches(fullpath);
    //get the values from the match group
    salesNo=matches[0].Groups[1].Value;
    date=matches[0].Groups[2].Value;
}