2

I have a string variable that holds the following information

@{[email protected]},@{[email protected]}

I am using this variable for a cmdlet that only accepts data in the format of

[email protected], [email protected]

I have tried TrimStart("@{EmailAddress=") but this only removes @{EmailAddress= for the first user and I guess TrimEnd would not be much use as I presume that it is due to the fact it reading the string as one line and not as user1,user2 etc.

Would anyone be able to provide advice on how to remove these unwanted characters.

2 Answers 2

3

A possible solution is to use Regex to extract the strings that you want, and combine them into a result:

$str = "@{[email protected]},@{[email protected]}"
$pattern = [regex]'@{EmailAddress=(.+?)}'
$result = ($pattern.Matches($str) | % {$_.groups[1].value}) -join ','

$result then is:

[email protected],[email protected]
Sign up to request clarification or add additional context in comments.

Comments

2

Another solution would be to use the -replace function. This is just a one-liner:

'@{[email protected]},@{[email protected]}' -replace '@{EmailAddress=([^}]+)}.*?', '$1'

Regex used to replace:

enter image description here

3 Comments

Thanks Martin, the diagram helped me understand - much appreciated
Hi Martin, Sorry to move the goalposts a bit here but is there an option to add double quotes to each of the users for example "[email protected]", "[email protected]" - I appreciate this may be an entirely different question requiring a different response but thought I would ask here first. Thanks
Sure, just replace '$1' with '"$1"'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.