我正在尝试使用Powershell从文本字符串中提取数据。我需要的数据在第一个和最后一个括号之间。到目前为止,我似乎可以使用,但如果数据本身包含右括号,则无法使用...
$MyText = "BT /F3 8.999 Tf 0 0 0 rg 407.446 TL 64.368 772.194 Td (\(TESTJulia\) Julia's Test Company) Tj T* ET"
[regex]::match($MyText,'(?<=\().+?(?=\))')
答案 0 :(得分:0)
这是您想要的吗?
$MyText = "BT /F3 8.999 Tf 0 0 0 rg 407.446 TL 64.368 772.194 Td (\(TESTJulia\) Julia's Test Company) Tj T* ET"
$match = [regex]::Match($MyText,'\(+?(.*)\)')
Write-Host $match.Captures.groups[1].value
输出:
\(TESTJulia\) Julia's Test Company
正则表达式说明(由Regex101.com提供):
\(+? matches the character ( literally (case sensitive)
+? Quantifier — Matches between one and unlimited times, as few times as possible, expanding as needed (lazy)
1st Capturing Group (.*)
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\) matches the character ) literally (case sensitive)
答案 1 :(得分:0)
这是到达目的地的一种略有不同的方法... [咧嘴]
这取决于td (
和) tj
始终存在,但是它可以与示例数据一起使用。
$InStuff = "BT /F3 8.999 Tf 0 0 0 rg 407.446 TL 64.368 772.194 Td (\(TESTJulia\) Julia's Test Company) Tj T* ET"
$InStuff -match 'td \((.+)\) tj .+$'
$Matches[1]
输出...
\(TESTJulia\) Julia's Test Company
答案 2 :(得分:0)
为什么不删除懒惰的量词?这会使它变得贪婪,因此它会捕捉到尽可能多的字符,直至达到前瞻性。
PS>$MyText = "BT /F3 8.999 Tf 0 0 0 rg 407.446 TL 64.368 772.194 Td (\(TESTJulia\) Julia's Test Company) Tj T* ET"
PS>[regex]::match($MyText,'(?<=\().+(?=\))')
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 55
Length : 35
Value : \(TESTJulia\) Julia's Test Company