1

In this example since its a space it matches it fine. But let's say a regioname doesn't have a space it matches the rest. I just want to match only stuff in the quotes.

'geoRegionName="Washington" geoCityName="Bonney Lake"' -match '(?<=geoRegionName=)\S+.*?"'

$matches = "Bonney Lake"

This works fine because of space

'geoRegionName="Washington " geoCityName="Bonney Lake"' -match '(?<=geoRegionName=)\S+.*?"'

"Washington "

But no space and it starts matching other items. Please help me to only match quotes, including cases where name may not be just one word.

'geoRegionName="Washington" geoCityName="Bonney Lake"' -match '(?<=geoRegionName=)\S+.*?"'

"Washington" geoCityName="

mklement0
  • 382,024
  • 64
  • 607
  • 775
Nerf D
  • 143
  • 1
  • 8

1 Answers1

1

Use subexpression [^"]+ to (greedily) match a nonempty run (+) of characters other than a " ([^"]), i.e. everything up to but not including the next ", whether or not those characters encompass whitespace:

$str = 'geoRegionName="Washington" geoCityName="Bonney Lake"'
if ($str -match '(?<=geoRegionName=")[^"]+') {
  $Matches[0]
}

Or, using a -replace operation:

$str = 'geoRegionName="Washington" geoCityName="Bonney Lake"'
$str -replace '^geoRegionName="([^"]+).+', '$1'

Both commands above yield (note that the enclosing " were removed, but could easily be included):

Washington

As for what you tried:

\S+ greedily matches a non-empty run (+) of non-whitespace characters (\S), and that includes " chars., so \S+.*?" kept matching beyond the immediate closing " until the next " following the first whitespace.

mklement0
  • 382,024
  • 64
  • 607
  • 775