简体   繁体   English

powershell-使用正则表达式之间提取值

[英]powershell - extract value in between using regex

I tried to extract content between 2 sections in Outlook MSG: 我试图提取Outlook MSG中2个部分之间的内容:

part A: A部分:

XXXXXXXXXXXXXX XXXXXXXXXXXXXX

part B: B部分:

XXXXXXXXXXXXXX XXXXXXXXXXXXXX

part C: C部分:

I'm using the regex "(?sm)part A:(.*?)part C:" . 我正在使用正则表达式"(?sm)part A:(.*?)part C:" I'm doing it like this because some of the msg doesn't have part B: . 我这样做是因为某些味精没有part B: Is there a way to remove/exclude the part B: content from the output? 有没有一种方法可以从输出中删除/排除part B:内容? Any help is really appreciated, thanks. 非常感谢您的任何帮助。

Yes, you add a separate capture block with (Part B: .*?)? 是的,您添加了一个单独的捕获块,并带有(Part B: .*?)? after the one you desire to capture. 在您想要捕获的那一个之后。 This block will only have data if your message has a "Part B". 仅当您的消息具有“ 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. 类似于Vespers的答案。 I would use $matches for this as well since your are looking to extract two parts. 由于您要提取两个部分,因此我也将使用$matches 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. $msg将包含邮件的内容,或者如果您使用的是Outlook com对象,则可以将$msg.Body放置在适当的位置。 ?<capturename> is how you use named matches so that way you can reference the matching property in the $matches object. ?<capturename>是使用命名匹配项的方式,因此您可以在$matches对象中引用匹配属性。 I also put in a \\s+ to remove one of the newlines that should be skipped in your example text. 我还输入\\s+来删除示例文本中应跳过的换行符之一。

You can also see the -f format operator used here. 您还可以在此处使用-f格式运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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