【发布时间】:2022-08-18 23:16:08
【问题描述】:
我有这个测试数据:
^Test data
This is all just test data
testing 123
ABC>space \"ABC\"
ABC>
我在 regex101.com 上设置了一个正则表达式
(^\\^|ERROR).*((|\\n|\\r|\\w|\\W)?)+(?=ABC>)
该表达式正在返回我在网站上想要的内容:
我正在使用我编写的这个 powershell 来获取与上述内容类似的内容并循环文件,并查找相同正则表达式的匹配项。
$files = gci \"\\\\server\\path\"
$content = @()
ForEach($file in $files){
# Set script name
$scriptname = \"ABC TEST_081722\"
# Get the name of the task for the logfile\'s filename.
$taskname = \"THIS IS A TEST!!!\"
# Create log file with a datestamp MMDDYY
$datestamp = (get-date).ToString(\'MMddyy\')
$logfilepath = \"\\\\server\\path\\Logs\\$($taskname)\\$($file.basename).log\"
$log_dir = \"\\\\server\\path\\Logs\\$($taskname)\\\"
# Get the content of the log file. We are only interested in getting lines which match a regex for our command line and our output line.
$content_raw = get-content $logfilepath -raw
$content_raw -match \"(^\\^|ERROR).*((|\\n|\\r|\\w|\\W)?)+(?=ABC>)\"
Write-host -f yellow $file.fullname
$matches
$matches.clear()
start-sleep -s 2
}
正则表达式在我的三个测试文件中的两个中找到匹配项,但不是第一个与我上面的示例具有完全相同的字符串内容的匹配项。为什么它在第二个和第三个文件中找到匹配但不是第一个?
第二个和第三个文件的内容是这样的
ABC>W !,MSG
ERROR^BATCH~Batch in use
ABC>space \"ABC\"
所以这两个文件没有以 \"^\" 符号开头的行。它以 \"ERROR\" 开头,我在正则表达式中使用 OR 语句说明了这一点。我只是不明白它如何能够找到以 \"ERROR\" find 开头的行,但没有从第一个以 \"^\" 克拉开头的文件中找到行。
标签: regex powershell