0

I am using the Jenkins console sections plugin [1] on a windows server. It is excellent in order to make a nice left navbar on my logs.

Positively, I would like any error message to cause a section header, eg;

Assert-PathExstsNotTooLong : ERROR, The path does not exist: E:\P...
...
Oops! Error, please do not do that.

Negatively, I would like to be able to avoid having spelled-out execution templates cause a new section header, eg the below.

[workspace] $ cmd.exe /C " c:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /p:Configuration=Debug /p:VisualStudioVersion=12.0 "E:\Program Files (x86)\Jenkins\jobs\M.sln"

Using references here on SO [2] and on the tester you recommended [3], I came up with the following, but it is not working?

^(?=(.*([Ee][Rr][Rr][Oo][Rr] ).*))(?!(%%ERRORLEVEL%%))

Using Regex101's amazing tester, with JS flavor, I used the above as input and had these test strings and outputs. The second line of match info perhaps explains my issue but I do not understand it.

test-strings = 
help error you should see me
i am %%errorlevel%% again
i am not a section

match-info; 
1.  `help error you should see me`
2.  `error `

Any tips? thank you!

1.[] ;This plugin uses Java Regex, per its docs ; ; ; ; X.Collapsing Console Sections Plugin - Jenkins - Jenkins Wiki ; ; https://wiki.jenkins-ci.org/display/JENKINS/Collapsing+Console+Sections+Plugin

2.[] ; An example regex on characters, not strings, to avoid; ; ; ; X.java - Regular expression include and exclude special characters - Stack Overflow ; ; Regular expression include and exclude special characters

3.[] ; ; ; ; ; X.Online regex tester and debugger: JavaScript, Python, PHP, and PCRE ; ; https://www.regex101.com/#javascript

1 Answer 1

1

(I can't add comments yet, otherwise I'd ask directly, but your example of a spelled-out message template doesn't include the text %%ERRORLEVEL%%, but I assume that it's meant to be a string with %%ERRORLEVEL%% somewhere in the middle of it. Also, as the example isn't quite right, I can't tell exactly what you mean by "not working")

Your problem is that your regex matches ERROR_ (with a space) anywhere in the text, except where the text is exactly %%ERRORLEVEL%%. I think that instead you could write:

^(?=(.*([Ee][Rr][Rr][Oo][Rr])))(?!.*(%%ERRORLEVEL%%)).*
  • Do you really need to only match ERROR_ (with a space) as opposed to ERROR (whether or not it has a space)? If the former, then you are already excluding %%ERRORLEVEL%%, and you could just use .*(?i:ERROR ).* as the full regex.
  • The Collapsing Console Sections Plugin uses Java regular expressions, so you can use (?i:ERROR) to match ERROR case-insensitively.
  • You need a trailing .* before and after your negative-lookahead atom for %%ERRORLEVEL%%, otherwise it will only exclude an exact match
  • The documentation for the plugin doesn't say whether the pattern has to match a line completely, or if it just matches text within the line. If it matches the line completely, the leading ^ is unnecessary, but won't be doing any harm.
  • You've got capturing brackets around ERROR and %%ERRORLEVEL%%. If you're not doing anything with that text, then those brackets are unnecessary.

The following regex will match any line with any of ERROR, Error, error etc in it, except lines with any of %%ERRORLEVEL%%, %%ErrorLevel%%, %%errorlevel%% etc.

^(?=.*(?i:ERROR))(?!.*(?i:%%ERRORLEVEL%%)).*
Sign up to request clarification or add additional context in comments.

2 Comments

ty @Jelaby! 1.I made a tweak and now I'm pretty happy with it: <pre>^(?=(.*([Ee][Rr][Rr][Oo][Rr])))(?!.*(%%ERRORLEVEL%%))(?!.*(may get compilation errors.))(?!.*(errorreport:prompt)).*</pre> ; 2.The reason I was using 'error ' instead of 'error' is that many times the word error is repeated alot on multiple lines and on non-error lines. Instead now I exclude the key non-errors.; 3.An online tester for Java java-regex-tester.appspot.com , since JS regex gives '(?' is 'invalid group structure'. Thank you so much!
It's quite likely that Java's is a different flavour of regular expression from Javascript's, and (?i:) is a Perl/Java-specific system. Regarding a tester: I've been testing with an actual Java development environment, so I can't really help you. A brief look at a Google search for "java regex tester" supplies a number of promising looking links. If the cases where "error" is followed by a non-word character, like space, colon etc, then you could use \b like (?i:ERROR\b). \b is matches a word-boundary - i.e. the change between a letter and a non-letter, or vice-versa.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.