【发布时间】:2021-10-03 17:55:18
【问题描述】:
我正在使用 powershell 在 txt 中使用正则表达式,但它仅在文本不包含回车时才有效。我准备了一个这样的示例文件:
the duck is on the table --found!
the elephant is on the table --found!
the cat is
on the table --NOT found! :-(
the lion is on the tablet --NOT found but ok ;-)
the dog is on
the table --NOT found! :-(
the turtle isonthe table --NOT found but ok ;-)
the cow is on the table --found!
我想要包含“在桌子上”的案例,所以我执行了这个:
select-string -path "c:\example.txt" -pattern '([^\w]{1})on([^\w])+the([^\w])+table([^\w]{1})'
这是输出:
example.txt:1:鸭子在桌子上——找到了!
example.txt:2:大象在桌子上——找到了!
example.txt:14:牛在桌子上——找到了!
但我也需要回车的情况!猫在哪里?狗在哪里?
谢谢你;-)
【问题讨论】:
-
在测试匹配之前尝试从字符串中删除任何回车/换行符。
标签: regex powershell select-string