简体   繁体   中英

Regex for extracting build number from Copy artifacts plugin in Jenkins

I have a Jenkins build that copies an artifact from another build.

Using description setter plugin I want to extract the build number from which I take the artifact.

The log file contains this line :

Copied 1 artifact from "Mybuild" build number 569

I need to use regex to extract only the integer 569 (can be any int). Also the 1 can be any int and Mybuild can be any string (but no spaces, one word). I can assume the words Copied, artifact, from, build, number are constant and always appear

I have tried to match a regex, but with no luck. Also tried some regex generator websites but no luck either.

Thanks.

You could probably use

Copied (\d+) artifact from "([^"]+)" build number (\d+)

and then use the third group (or remove the parentheses around the first two captured tokens and use group number 1).

\\d refers to any decimal digit, the + trailing it is a so-called quantifier that will try matching the previous token (the digit in this case) at least one times and as often as possible. [^"] is a character class containing any character that is not a double quote. This way we can make sure that we catch everything within the quotes (not strictly necessary here, but a good pattern to keep in mind). All the rest is just matched verbatim.

Quick PowerShell test:

PS> 'Copied 1 artifact from "Mybuild" build number 569' -match 'Copied (\d+) artifact from "([^"]+)" build number (\d+)'
True
PS> $Matches

Name                           Value
----                           -----
3                              569
2                              Mybuild
1                              1
0                              Copied 1 artifact from "Mybuild" build number 569

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