Reputation: 23
I'm having some issues pulling the desired values from my source string.
I have 2 possible string formats (that I'm differentiating based on a -match operation and if check):
{u'specialGroup': u'projectWriters', u'role': u'WRITER'
or
, {u'role': u'WRITER', u'userByEmail': u'[email protected]'
What I desire to return from the regex:
[0]projectWriters
[1]WRITER
and
[0]WRITER
[1][email protected]
Basically I need to return all values between start string : u'
and end string '
as array values [0] and [1] but cannot figure out the regex pattern.
Trying:
[regex]::match($stuff[1], ": u'([^']+)'").groups
Groups : {: u'WRITER', WRITER}
Success : True
Captures : {: u'WRITER'}
Index : 10
Length : 11
Value : : u'WRITER'
Success : True
Captures : {WRITER}
Index : 14
Length : 6
Value : WRITER
But no sign of [email protected]
value.
Upvotes: 0
Views: 80
Reputation: 438123
A pragmatic approach, assuming that all strings have the same field structure:
$strings = "{u'specialGroup': u'projectWriters', u'role': u'WRITER'}",
", {u'role': u'WRITER', u'userByEmail': u'[email protected]'"
$strings | ForEach-Object { ($_ -split 'u''|''' -notmatch '[{}:,]')[1,3] }
yields:
projectWriters
WRITER
WRITER
[email protected]
As for what you tried:
[regex]::match()
only ever returns one match, so you need to base your solution on [regex]::matches()
- plural! - which returns all matches, and then extract the capture-group values of interest.
$strings | ForEach-Object { [regex]::matches($_, ": u'([^']+)'").Groups[1,3].Value }
Upvotes: 1