regex - 正则表达式不适用于在 word 文档的范围内查找
问题描述
正则表达式不工作,要提取两个部分之间的内容(功能工作正常,但可能我没有提到正确的正则表达式来查找)
ExtractFromWordDoc"D:\Scan.doc" '(?:\d{2}\.\d).*(?:Non-Payment)' '(?:\d{2}\.\d).*(?:Financial covenants and other obligation)'
Word文档内容(需要提取29.1和29.2之间的信息)
29.1 不付款
债务人不会在到期日支付任何根据财务文件在其表示为应付的地点和货币支付的应付款项,除非:
(a) 其未能付款的原因是: (i) 行政或技术错误;(b) [付款在以下期限内进行:(i)(在上述 (a)(i) 段的情况下),到期日的 [ ] 个工作日;
29.2 财务契约和其他义务
(a) 第 27 条(财务契约)的任何要求未得到满足[或债务人不遵守第 26 条(信息承诺)][和/或第 28 条(一般承诺)的规定]。
function ExtractFromWordDoc{
Param([string]$SourceFile, [string]$SearchKeyword1, [string]$SearchKeyword2)
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.Documents.Open($SourceFile,$false,$true)
$sel = $word.Selection
$paras = $doc.Paragraphs
foreach ($para in $paras)
{
if ($para.Range.Text -match $SearchKeyword1)
{
$startPosition = $para.Range.Start
}
if ($para.Range.Text -match $SearchKeyword2)
{
$endPosition = $para.Range.Start
break
}
}
[array]$content=New-Object System.Collections.ArrayList
$doc.Range($startPosition, $endPosition).Copy()
$content=Get-Clipboard -Raw
$content = $content -replace "'", ""
# cleanup com objects
$doc.Close()
$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
解决方案
您在正则表达式中只有一个小错误。
示例文本Non-payment
但正则表达式匹配Non-Payment
(区分大小写)
如果你改变'(?:\d{2}\.\d).*(?:Non-Payment)'
它'(?:\d{2}\.\d).*(?:Non-payment)'
应该工作。
另一个注意事项是您缺少s
from obligations
in(?:\d{2}\.\d).*(?:Financial covenants and other obligation)
但我不预见它会导致问题。
免责声明:我没有测试你的代码,只有你的正则表达式。
编辑:
我测试了以下
function ExtractFromWordDoc{
Param([string]$SourceFile, [string]$SearchKeyword1, [string]$SearchKeyword2)
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.Documents.Open($SourceFile,$false,$true)
$sel = $word.Selection
$paras = $doc.Paragraphs
foreach ($para in $paras)
{
if ($para.Range.Text -match $SearchKeyword1)
{
#"Point 1"
$startPosition = $para.Range.Start
}
if ($para.Range.Text -match $SearchKeyword2)
{
#"Point 2"
$endPosition = $para.Range.Start
break
}
}
[array]$content=New-Object System.Collections.ArrayList
$doc.Range($startPosition, $endPosition).Copy()
$content=Get-Clipboard -Raw
$content = $content -replace "'", ""
# cleanup com objects
$doc.Close()
$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
ExtractFromWordDoc "C:\testing\test.doc" '(?:\d{2}\.\d).*(?:Non-payment)' '(?:\d{2}\.\d).*(?:Financial covenants and other obligation)'
剪贴板中的输出是:
29.1 Non-payment
An Obligor does not pay on the due date any amount payable pursuant to a Finance Document at the place at and in the currency in which it is expressed to be payable unless:
(a) its failure to pay is caused by: (i) administrative or technical error; or (b) [payment is made within: (i) (in the case of paragraph (a)(i) above), [ ] Business Days of its due date;
如果我添加$content
到函数的末尾,它会将此文本输出到控制台。
推荐阅读
- linux - go: 当我作为非 root 用户具有正确的能力时,无法打开和写入 /proc/os.Getpid()/oom_score_adj
- verilog - 如何修复“可能从不完整的案例或 if 语句生成闩锁”消息?
- python - newshape (, -1) 有什么作用?
- swift - 通过 Swift 泛型初始化 SwiftUI 视图
- python - 知道了速度场,我如何才能找到该速度场内“无质量”粒子的轨迹?
- flutter - 错误:NoSuchMethodError:在 null 上调用了方法“[]”。接收者:null 尝试调用:[]("name")
- angular - 使用来自 typescript 的 html 元素以角度包装组件
- javascript - 我想为某些数字和字符创建一个正则表达式,这些数字和字符多次出现并且 [number-number] 出现 0 次或多次。下面是我的正则表达式
- powershell - 通过 Powershell 将 office365 组添加到 Sharepoint 网站集管理员
- javascript - 如何使用自动逗号计算输入