正则表达式用于获取准确的git电子邮件配置

时间:2019-05-30 07:01:19

标签: regex

这是git show -s HEAD的输出

smilyface@smilingface /d/workspace/shirt (develop)
$ git show -s HEAD
commit f601411d4be3c3fbafea79c72ecc84ad78219265
Author: Smily Face <v-smily@somedomaingoeshere.com>
Date:   Wed May 29 15:22:45 2019 +0530

    Ticket-249843 : [shirt team apps] Deprecation of x function in Jenkinsfile

这是我尝试过的-

尝试1:

git show -s HEAD | grep "Author:" | grep -Po '(?<=(<)).*(?=@)'

输出:
v-smily

问题:
有时,电子邮件可能以with v-开头,有时是without v-

例如-期望发送电子邮件和输出
abcd@somedomaingoeshere.com-> abcd
v-xyz@somedomaingoeshere.com-> xyz
v-pqr@somedomaingoeshere.com-> pqr

1 个答案:

答案 0 :(得分:1)

如果您想坚持使用PCRE,可以使用

GlobalVar

libpatch.so模式与git show -s HEAD | grep -oP 'Author:.*<(v-)?\K[^@]+' 匹配,直到最右边的Author:.*<(v-)?\K[^@]+的任意0+字符都可以,其后跟Author:,然后使用<从匹配缓冲区中删除所有匹配的文本,然后v-匹配\K以外的1个以上字符。

请参见regex demoonline grep demo

或者,使用

[^@]+

提取一个字符串,该字符串以@,任意0+个字符,git show -s HEAD | grep -oE 'Author:.*<[^@]+' | sed -E 's/Author:.*<(v-)?//' 和随后的1+个字符(除Author:之外)开头,然后使用<删除您所做的操作不需要,从@sed到可选Author:的所有字符串。