Append character after each instance of a regex match in PowerShell

1.4k views Asked by At

I was wondering if it was possible to append a character (to be used as a delimiter later on) to each instance of a regex match in a string.

I'm parsing text for a string between < >, and have a working regex pattern -- though this collapses each instance of the match.

What I would like to do is append each instance of a match with a , so I can call the .split(',') method later on and have a collection of string I can loop through.

$testString = "<[email protected]><[email protected]>"
$testpattern = [regex]::Match($testString, '(?<=<)(.*)(?=>)').Value

$testPattern will now be "[email protected]@gmail.com"

What I would like to is to add a delimiter between each instance of the match, to call the .split() method to work with a collection after the fact.

2

There are 2 answers

0
AudioBubble On BEST ANSWER

I know this isn't the only way to handle the problem above, and definitely not the most efficient -- but I ended up doing the following.

So to restate the question, I need to parse email headers (to line), for all the smtp addresses (value between '<' and '>'), and store all the addresses in a collection after the fact.

        $EMLToCol = @()

        $parseMe = $CDOMessage.to
        # select just '<emailAddress>'
        $parsed = Select-String -Pattern '(<.*?>)+' -InputObject $parseMe -AllMatches | ForEach-Object { $_.matches }
        # remove this guy '<', and this guy '>'
        $parsed = $parsed.Value | ForEach-Object {$_ -replace '<' -replace '>'}
        # add to EMLToCol array
        $parsed | ForEach-Object {$EMLToCol += $_}
0
None On

$testpattern is [email protected]><[email protected]

You should use <(.*)><(.*)> to keep both email address and then concatenate both strings: $testpattern = $testpattern[0] + "your string you want inbetween" + $testpattern[1]

Not sure about 0 and 1, depends on the language.

Another point, be carefull, if there are some spaces or invalid characters for email, it'll still capture them. you should use something like <([a-zA-Z0-9\-@\._]*\@[a-zA-Z0-9-]*\.[a-z-A-Z]*)><([a-zA-Z0-9\-@\._]*\@[a-zA-Z0-9-]*\.[a-z-A-Z]*)>