Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell get substring from string regexp

There is a string "Paul Cervov <paul.cervov@example.com>".
I want to get just username from email address.

In bash we can do so:

COMMIT_AUTHOR_NAME=$(echo "$CI_COMMIT_AUTHOR" | sed "s/.*\<\(.*\)\@.*/\1/g")

How can I do same with PowerShell?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use PowerShell -replace regex operator:

"Paul Cervov <paul.cervov@example.com>" -replace '.*<(.*)@.*', '$1'
# or, with variables:
$COMMIT_AUTHOR_NAME = $CI_COMMIT_AUTHOR -replace '.*<(.*)@.*', '$1'

As you can see, the only real difference is that .NET’s regex engine uses $1 instead of \1 to refer to the 1st capture group in the substitution string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading