简体   繁体   中英

powershell - extract value in between using regex

I tried to extract content between 2 sections in Outlook MSG:

part A:

XXXXXXXXXXXXXX

part B:

XXXXXXXXXXXXXX

part C:

I'm using the regex "(?sm)part A:(.*?)part C:" . I'm doing it like this because some of the msg doesn't have part B: . Is there a way to remove/exclude the part B: content from the output? Any help is really appreciated, thanks.

Yes, you add a separate capture block with (Part B: .*?)? after the one you desire to capture. This block will only have data if your message has a "Part B".

PS K:\> $t="Part A: blabla Part B: bla Part C: bla"
PS K:\> $regex="(?ms)Part A: (.*?)(Part B:.*?)?Part C:"
PS K:\> $t -match $regex
True
PS K:\> $matches

Name                           Value
----                           -----
2                              Part B: bla
1                              blabla
0                              Part A: blabla Part B: bla Part C:


PS K:\> $tt="bla Part A: no wai Part C: here"
PS K:\> $tt -match $regex
True
PS K:\> $matches

Name                           Value
----                           -----
1                              no wai
0                              Part A: no wai Part C:

Similar to Vespers answer. I would use $matches for this as well since your are looking to extract two parts. I am going to use named matches and a slightly different regex pattern.

$pattern = "(?sm)part A:(?<betweenAB>.*?)\s+part B:(?<betweenBC>.*?)part C:"
If($msg -match $pattern){
    "{0}{1}" -f $matches.betweenAB, $matches.betweenBC 
}

$msg would contain the content of your message or you could put in place $msg.Body if you are using Outlook com object. ?<capturename> is how you use named matches so that way you can reference the matching property in the $matches object. I also put in a \\s+ to remove one of the newlines that should be skipped in your example text.

You can also see the -f format operator used here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM