I'm trying to run a Jenkins build step based on a conditional regular expression match and I can't figure out why it doesn't match.
Here's how it looks in Jenkins:
I'm using this regular expression: .*iOS. against a build parameter, pmt_content.
Here's the pmt_content value:
<body><pre>^1234567890^abc123def132afd1213afas^iOS^Test User^test#abc.com^iPad 3^</pre><img src='https://cirrus.app47.com/notifications/5626a0bc9ac25b6ea7003f2f/img' alt=''/></body>
Here's the console log telling me it did not match.
[PollMailboxTrigger] An email matching the filter criteria was found. (log)
Building in workspace /Users/jenkins/.jenkins/jobs/New Device Listener/workspace
Run condition [Regular expression match] enabling prebuild for step [Execute shell]
Regular expression run condition: Expression=[.*iOS.*], Label=[
<body><pre>^1234567890^abc123def132afd1213afas^iOS^Test User^test#abc.com^iPad 3^</pre><img src='https://cirrus.app47.com/notifications/5626a0bc9ac25b6ea7003f2f/img' alt=''/></body>]
Run condition [Regular expression match] preventing perform for step [Execute shell]
Finished: SUCCESS
Clearly pmt_content contains "iOS".
I tested the same values at www.regexplanet.com.
What am I doing wrong?
I experienced similar frustrations due to a newline character in my label data which prevented a match. Hard to tell from your dump if there might be a newline in there at the start. In any event you might try (?is).*iOS.*. Reference.
Related
I've been trying to get the right regex for skipping the first word and parsing the rest of the message.
I've been testing the regex by running Logstash locally
grok {
match => { "resource" => "/[^/]+/[^/]+(/|)(?<repo>[^/]+)?(/%{GREEDYDATA:resource_path})?" }
}
Test Messages:
/list/Lighter-test-group/xyz/123
/list/
/list
For messages,
/list/Lighter-test-group/xyz/123 gives us repo value as "Lighter-test-group" which is valid
/list/ gives us repo value as null which is valid
but /list gives repo value as "list" which is an invalid value. The correct value needs to be empty or null.
Not sure if you are restricted to using one really long regex but I would look into custom patterns to ignore the first word.
Using this grok debugger, I setup some custom patterns in the 3rd box:
IGNORE /\b\w+\b
REPO [A-Za-z]([A-Za-z0-9+\-.]+)+
And tested out this grok pattern in the 2nd box:
%{IGNORE}(/)?(%{REPO:repo})?(%{GREEDYDATA:resource_path})
Using these custom patterns, I was able to get what I think is your desired output but test them out with more use cases if you have any.
I'm using Jenkins with a branch parameter to specify the branch to build from. Other stuff downstream needs the branch name to not have the leading "origin/" -- just "feature/blahblah" or "bugfix/12345" or similar. The advanced settings for the parameter let me specify a branch filter via regex, but I'm a regex newbie and the solutions I've found in searching are language-dependent. The Jenkins documentation for this is sparse.
When a user clicks on "build with parameters", for the branch I want to see branch names that omit the leading "origin/". I'm not sure how to write a regex for Jenkins that will "consume" that part of the branch name before setting the parameter value.
I solved this problem once before, I'm pretty sure using Stack Overflow, but I can't find those hints now.
For the git branch parameter, set Branch Filter to:
origin/(.*)
I found the parentheses to be counter-intuitive, because if you don't specify a filter you get:
.*
(No parens.) If you are filtering stuff out, you use parens to indicate the part to keep.
I usually use a groovy script evaluated before the job, like:
def map = [:]
map['GIT_BRANCH'] = GIT_BRANCH - 'origin/'
return map
This is using the EnvInject plugin, as described in gitlab-plugin issue 444
If you need to filter multiple patterns without origin/ section, try the following.
origin/(develop.*|feature.*|bugfix.*)
This will list the develop, feature and bugfix branches without the leading origin/.
I am trying to search thru log files to see if any warnings have appeared so that I can warn in a Jenkins pipeline using Jenkins plug in "Text Finder".
However, I have a case where I do not want hits on the string "CRIT" int he logfile if the string also contains plms.
E.g.
I have the following text in the log file:
<CRIT> 23-Jun-2014::10:57:13.649 Upgrade committed
<CRIT> 23-Jun-2014::10:57:13.703 no registration found for callpoint plmsView/get_next of type=external
I am not interested in having a warning for the second line, so I have added the following regex to Text Finder in Jenkins:
WARN|ERROR|<ERR>|/^(?=<CRIT>)(?=^(?:(?!plms).)*$).*$/
This should get a hit on CRIT only if the string does not also contain plms, i.e the first line, but I do not get a hit on either line.
I got the code from here: Combine Regexp
Could someone please help me correct this? Thanks!
You should use something like this:
WARN|ERROR|<ERR>|<CRIT>(?!.*?no registration found)
Change the no registration found part to match the <CRIT> message you want to exclude.
This expression matches also for the line:
<INFO> User WARNER registered
so you should consider using something like:
^(WARN|ERROR|<ERR>|<CRIT>(?!.*?no registration found))
that matches only if the tokens are at the beginning of the line (change the tokens accordingly).
This should work for you:
^<CRIT>(.(?!plms))*$
Demo and explanation
I am using the maven replacer plugin and I've run into a situation where I have a regular expression that matches across lines which I need to run on the input file until all matches have been replaced. The configuration for this expression looks like this:
<regexFlags>
<regexFlag>DOTALL</regexFlag>
</regexFlags>
<replacements>
<replacement>
<token>\#([^\n\r=\#]+)\#=([^\n\r]*)(.*)(\#default\.\1\#=[^\n\r]*)(.*)</token>
<value>#$1#=$2$3$5</value>
<replacement>
<replacements>
The input could look like this:
#d.e.f#=y
#a.b.c#=x
#h.i.j#=aaaa
#default.a.b.c#=QQQ
#asdfasd.fasdfs.asdfa#=23423
#default.h.i.j#=234
#default.RR.TT#=393993
and I want the output to look like this:
#d.e.f#=y
#a.b.c#=x
#h.i.j#=aaaa
#asdfasd.fasdfs.asdfa#=23423
#default.RR.TT#=393993
The intention is to re-write the file, but without the tokens with a #default prefix, where another token without the prefix has already been defined.
#default.a.b.c#=QQQ and #default.h.i.j#=234 have been removed from the output because other tokens already contains a.b.c and h.i.j.
The current problem I have is that the replacer plugin only replaces the first match, so my output looks like this:
#d.e.f#=y
#a.b.c#=x
#h.i.j#=aaaa
#asdfasd.fasdfs.asdfa#=23423
#default.h.i.j#=234
#default.RR.TT#=393993
Here, #default.a.b.c=QQQ is gone, which is correct, but #default.h.i.j#=234 is still present.
If I were writing this in code, I think I could probably just loop while attempting to match on the entire output, and break when there are no matches. Is there a way to do this with the replacer plugin?
Edit: I may have over simplified my example. A more realistic one is:
#d.e.f#=y
#a.b.c#=x
#h.i.j#=aaaa
#default.a.b.c#=QQQ
#asdfasd.fasdfs.asdfa#=23423
#default.h.i.j#=234
#default.RR.TT#=393993
#x.y.z#=0
#default.q.r.s#=1
#l.m.n#=8.3
#q.r.s#=78
#blah.blah.blah#=blah
This shows that it's possible for a default.x.x.x=y to precede a x.x.x=y token (as #default.q.r.s#=1 preceedes #q.r.s#=78`), my prior example wasn't clear about this. I do actually have an expression to capture this, it looks a bit like this:
\#default\.([^\n\r=#|]+)#=([^\n\r|]*)(.*)#\1#=([^\n\r|]*)(.*)
I know line separators are missing from this even though they were in the other one - I was experimenting with removing all line separators and treating it as a single line but that hasn't helped. I can resolve this problem simply by running each replacement multiple times by copying and pasting the configurations a few times, but that is not a good solution and will fail eventually.
I don't believe you could solve this problem as is, a work-around is to reverse the order of the file top to bottom, perform lookahead regex and then reverse the result order
pattern = #default\.(.*?)#[^\r\n]+(?=[\s\S]*#\1#) Demo
another way (depending on the capabilities of "Maven") is to run this pattern
#(.*)(#[\s\S]*)#default\.\1.*
and replace with #$1$2 Demo in a loop until there are no matches
then run this pattern
#default\.(.*)#.*(?=[\s\S]*\1)
and replace with nothing Demo in a loop until there are no matches
It doesn't look like the replacer plugin can actually do what I want. I got around this by using regular expressions to build multiple filter files, and then applying them to the resource files.
My original goal had been to use regular expressions to build a single, clean, and tidy filter file. In the end, I discovered that I was able to get away with just using multiple filters (not as clean or tidy) and apply them in the correct order.
I have a log file in which I have DEBUG, NORMAL and CRITICAL entries as well as some info that does not start with a regular (for this type of logs) data e.g. [20130313:123412]
[210313:100114] NORMAL: this is normal log
[210313:100114] DEBUG: ../../common/
Additional info:
number of ....
I would like to remove both DEBUG entries as well as those that do not start with [
I know I can do that with:
:g/DEBUG/d
and
:g!/^\[/d
how can I combine this into one ? Or properly use a regex ?
Convert them both to positive or negative rules (as appropriate), and then you can use \| ("or") to match one or the other.
:g/^[^\[]\|DEBUG/d
That would do it. ^[^\[] for lines starting with other than [, or lines containing DEBUG.