正则表达式代码从文件路径字符串中提取多个信息

时间:2017-09-27 00:14:53

标签: regex

任何人都可以帮助我使用正则表达式代码来提取2010gfHS&来自以下字符串的cd

E:\DWP\DS\Planning Analysis\Projects\2012_Tableau Visualisation Program\4. Strategys Models\BMTS Model\JnnnZzzzz_model results\2010\gfHScd.out 

谢谢:)

1 个答案:

答案 0 :(得分:1)

就是这样,我重新发表评论的答案:

\\(\d{4})\\(\w{2})(\w{2})(\w{2})\.

const regex = /\\(\d{4})\\(\w{2})(\w{2})(\w{2})\./g;
const str = `E:\\DWP\\DS\\Planning Analysis\\Projects\\2012_Tableau Visualisation Program\\4. Strategys Models\\BMTS Model\\JnnnZzzzz_model results\\2010\\gfHScd.out 
`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}