如何使用正则表达式测试文件是否存在

时间:2017-01-20 01:57:25

标签: powershell

我想测试我的磁盘上是否存在YYYYMMDD表示年,月和日期的文件C:\workspace\test_YYYYMMDD.txt

我如何在PowerShell中执行此操作?

我知道test-path test_*.txt命令返回true。

但是当文件名类似于 test_20170120asdf.txt test_2015cc1119aabb.txt 时,test_*.txt也会返回true。

我不希望在test-path中将 test_20170120asdf.txt 等文件名标记为true。

我想在test_\d{8}\.txt中应用正则表达式test-path。如何在PowerShell中执行此操作?

2 个答案:

答案 0 :(得分:3)

类似的东西:

accepts_nested_attributes_for :allergies, reject_if: ->(allergy){ allergy['description'].blank? && allergy['symptoms'].blank? }

答案 1 :(得分:1)

通配符表达式的匹配能力比正则表达式更受限制 - 请参阅Get-Help about_Wildcards - 但在这种特殊情况下,它们足够了:

std::string parenthetical(const std::string& s){
    const string foo;
    const auto start = find(foo.cbegin(), foo.cend(), '(');
    const auto finish = find_if(start, foo.cend(), [](char i) {
        if (i == '(') {
            count++;
        }else if (i == ')') {
            count--;
        }return count <= 0;});
    return 0;
}

如果需要更复杂的匹配,请参阅LotPing's answer,其中显示了如何使用正则表达式

相关问题