Regex Powershell解析

Regex Powershell解析,regex,powershell,capture-group,Regex,Powershell,Capture Group,我从GIT日志中获得以下输入: Merge: d9335ae 7d12d50 Author: name\name <mail@mail.com> Date: Wed Oct 31 12:55:00 2018 -0500 id:202847 Merge branch 'release/2.6.0' into release/3.0.0 # Conflicts: # configuration/path/path PowerShell具有非常强大的本机Regex支持,您可以

我从GIT日志中获得以下输入:

Merge: d9335ae 7d12d50
Author: name\name <mail@mail.com>
Date:   Wed Oct 31 12:55:00 2018 -0500

id:202847 Merge branch 'release/2.6.0' into release/3.0.0

# Conflicts:
#   configuration/path/path

PowerShell具有非常强大的本机Regex支持,您可以非常轻松地从git命令中检索ID值,如下所示。我们首先捕获git命令的输出在我的例子中,我将它粘贴到一个var中,但您也可以运行$commitsg=git commit 202847以另一种方式将输出捕获到一个var中:

$r="Merge: d9335ae 7d12d50
Author: name\name <mail@mail.com>
Date:   Wed Oct 31 12:55:00 2018 -0500

id:202847 Merge branch 'release/2.6.0' into release/3.0.0

# Conflicts:
#   configuration/path/path"
您可以将该输出存储在变量中,如下所示:

$CommitID = [regex]::Match($r, "id:(.....)\w+").Value
然后使用字符串扩展将其嵌入到其他命令中,如下所示:

"TP $CommitID Professional Lines: 2,800,000 Policy Aggregate Limit update"
>TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update

要提供更具PowerShell风格的替代方案,请执行以下操作:

# Sample log text (multi-line string in the form of a here-string).
$logText = @'
Merge: d9335ae 7d12d50
Author: name\name <mail@mail.com>
Date:   Wed Oct 31 12:55:00 2018 -0500

id:202847 Merge branch 'release/2.6.0' into release/3.0.0

# Conflicts:
#   configuration/path/path
'@

# Extract the ID number alone, via a capture group `(...)`, using the
# -match regex operator and the automatic $Matches variable that reflects the 
# results.
# Entry 1 of $Matches contains the 1st (and here only) capture-group value.
# \b is used to make the regex more robust, by only matching at word boundaries.
# With the sample input, $id receives value '202847'
$id = if ($logText -match '\bid:(\d+)\b') { $Matches[1] }

# Note: If your input comes directly from a *file*, say 'commit.log', 
#       use the following command instead:
#
#  $id = (Select-String -list '\bid:(\d+)\b' commit.log).Matches.Groups[1].Value
#
# Alternatively, if reading the whole file into memory at once is acceptable,
# pass (Get-Content -Raw commit.log) instead of $logText to -match.

# Build the desired output string from the ID obtained and the API return value.
"TP Id:$id " + $returnValFromApi

太好了——这正是我需要的——但有一个问题。当API只接受诸如202847之类的编号时,它将id:xxxxx传递到我的目标进程API中。我如何让正则表达式只修补以下6位数字:?抱歉-我对regex还不熟悉,并且边走边学习:我想你指的是git show,而不是git commit。正则表达式id:……\w+与其描述不匹配:您正在捕获id:,后跟5个字符,后跟一个或多个单词字符;你的意思是像id:.{6}\b这样的东西吗?另外,我不认为这里讨论的ID是Git提交ID;它们似乎是提交消息的一部分,因此OP自己的正则表达式在本质上可能就足够了:id:\d+奇怪的巧合是,您在我自己的答案之后提供了另一个答案,然后我的答案现在被否决了。“非常奇怪。这不是巧合,@FoxDeploy;事实上,这是我的标准MO:a如果我没有看到满意的答案,我会发布我自己的。b独立地说,如果我发现别人的答案有问题,我会留下一条评论,努力参与对话,希望得到正确的答案。一般来说,可能没有什么需要纠正的,但这只会在关于评论的对话过程中出现。如果在一段合理的时间后,我没有得到任何回应,我就投反对票。这就是这里发生的事情。有什么奇怪的吗?@FoxDeploy:如果达成共识并相应更新答案,我总是很乐意撤销否决票,甚至将其转换为赞成票。嗨,事后看来,我认为你对这个问题给出了一个非常好的答案,没有什么不快的感觉:。我想知道您是否知道哪个git命令实际生成了他给我们的输出?我一直在git show和其他命令中搜寻,找不到一个提供id:message here格式的命令。我感谢您的安抚性反馈,@FoxDeploy。我认为所讨论的ID与Git无关,只是碰巧是用户选择的提交消息的一部分,可能它链接到Git之外的系统。
"TP $CommitID Professional Lines: 2,800,000 Policy Aggregate Limit update"
>TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
# Sample log text (multi-line string in the form of a here-string).
$logText = @'
Merge: d9335ae 7d12d50
Author: name\name <mail@mail.com>
Date:   Wed Oct 31 12:55:00 2018 -0500

id:202847 Merge branch 'release/2.6.0' into release/3.0.0

# Conflicts:
#   configuration/path/path
'@

# Extract the ID number alone, via a capture group `(...)`, using the
# -match regex operator and the automatic $Matches variable that reflects the 
# results.
# Entry 1 of $Matches contains the 1st (and here only) capture-group value.
# \b is used to make the regex more robust, by only matching at word boundaries.
# With the sample input, $id receives value '202847'
$id = if ($logText -match '\bid:(\d+)\b') { $Matches[1] }

# Note: If your input comes directly from a *file*, say 'commit.log', 
#       use the following command instead:
#
#  $id = (Select-String -list '\bid:(\d+)\b' commit.log).Matches.Groups[1].Value
#
# Alternatively, if reading the whole file into memory at once is acceptable,
# pass (Get-Content -Raw commit.log) instead of $logText to -match.

# Build the desired output string from the ID obtained and the API return value.
"TP Id:$id " + $returnValFromApi