0

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.

1 Answer 1

2

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
1
  • 1
    If you want to learn more: regular-expressions.info is a very good resource, both for learning and for reference. It also explains quite well how the regex engine performs the matching for many constructs.
    – Joey
    Jul 17, 2012 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.