chuacw
chuacw

Reputation: 1863

How to match a string enclosed in quotes

I have a string, $line, where the contents are:

Filename="Longfilename"

I am trying to work out a regex to extract the string. I tried this:

$line -match "Filename=\"(?<TheFilename>[^\"]+)\"

where I am trying to capture Longfilename into $matches['TheFilename']

Unfortunately, this doesn't work.

How do I do this? Where is my mistake?

Upvotes: 1

Views: 3021

Answers (1)

Akim
Akim

Reputation: 8679

Seems that you did everying correctly, but add Groups property

$line = 'Filename="Longfilename"'
$matches = [regex]::Match($line, 'Filename=\"(?<TheFilename>[^\"]+)\"')
$matches.Groups['TheFilename']

Upvotes: 2

Related Questions