Tommy Sharp
Tommy Sharp

Reputation: 95

Regex match to extract data between first ( and last ) incase the data contains brackets?

I am trying to extract data from a string of text using Powershell. The data I need is between the first and last bracket. What I have so far appears to work but doesn't work if the data itself contains a close bracket...

$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,'(?<=\().+?(?=\))')

Upvotes: 0

Views: 199

Answers (3)

Chiranjib
Chiranjib

Reputation: 1783

Is this what you want?

$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

Output:

\(TESTJulia\)  Julia's Test Company

Regex explanation (courtesy 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)

Upvotes: 0

StephenP
StephenP

Reputation: 4081

Why not remove the lazy quantifier? This will make it greedy so that it grabs as many characters as it can unti it hits the lookahead.

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

Upvotes: 0

Lee_Dailey
Lee_Dailey

Reputation: 7479

here's a slightly different way to get there ... [grin]

it depends on the td ( and ) tj being always there, but it works with your sample data.

$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]

output ...

\(TESTJulia\)  Julia's Test Company

Upvotes: 0

Related Questions