Issue with RegEx for project roles in Matrix Authorization Strategy Plugin

983 views
Skip to first unread message

Jan Seidel

unread,
Sep 19, 2014, 3:38:17 AM9/19/14
to jenkins...@googlegroups.com
Hi all,

I have a weird issue with a regular expression.
Hopefully you can help me out here.

I have a RegEx like (.*)GUIDE_(?!MIB)(.*)(?!P4)
It should, as far as I can tell, list all jobs containing GUIDE but exclude GUIDE jobs with a MIB in the middle or P4 at the end of the project name, right?

My problem here is, that jobs with a P4 at the end still are listed :(

I have for testing purposes tried all kind of RegEx that crossed my mind but without any success.
Later I tried just to exclude job names with a P4 at the end. No joy

The RegEx I used was: (.*)GUIDE_(.*)(?!P4)
The list is then completely emtpy.

Can someone tell me where I glitch?

Cheers
Jan

Gunnar Strand

unread,
Sep 19, 2014, 3:57:37 AM9/19/14
to jenkins...@googlegroups.com
Hi Jan,


On 09/19/14 09:38, Jan Seidel wrote:
Hi all,

I have a weird issue with a regular expression.
Hopefully you can help me out here.

I have a RegEx like (.*)GUIDE_(?!MIB)(.*)(?!P4)
It should, as far as I can tell, list all jobs containing GUIDE but exclude GUIDE jobs with a MIB in the middle or P4 at the end of the project name, right?

No, the regex can consume "GUIDE_P4" using "GUIDE_(.*)" which is then not followed by "P4" and so is true. I am not aware of any way to write one expression to do what you want. You need a separate test for excluding names with "P4" at the end.



My problem here is, that jobs with a P4 at the end still are listed :(

I have for testing purposes tried all kind of RegEx that crossed my mind but without any success.
Later I tried just to exclude job names with a P4 at the end. No joy

The RegEx I used was: (.*)GUIDE_(.*)(?!P4)
The list is then completely emtpy.

That is strange since it should match all names containing "GUIDE_". I am unsure of the context, but your code should be something like this pseudo code:

if ( name != /P4$/ ) {
  println name;
}

BR
Gunnar

Gunnar Strand

unread,
Sep 19, 2014, 4:01:13 AM9/19/14
to jenkins...@googlegroups.com
Hi,

I did of course not read the subject. Shame on me.

The answer below is for regex in general, not sure how applicable it is to the Matrix Authorization Strategy Plugin (not using it).

BR
Gunnar
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Seidel

unread,
Sep 19, 2014, 5:27:05 AM9/19/14
to jenkins...@googlegroups.com
True (.*) is very greedy, but (?!P4) is a but not P4 and I would expect it to work.
It isn't as I just figured out in a test run with a completely different RegEx to skim for other jobs :/

I got to tweak it and create a set of project rules and then combine them for each user depending on what they need to know *geesh*
Thanks for the hint.

Jan

Jan Seidel

unread,
Sep 19, 2014, 5:44:53 AM9/19/14
to jenkins...@googlegroups.com
Yay, got it.
A set of projects rules. each one containing a positve match and then combine the rule works well.

One odd thing I learned is, that you don't need wildcards if your match has a trailing string.

e.g. MB_GUIDE_Pxxxxxxx_P4_DELIVERY will also be found when you work with (.*)GUIDE_(.*)P4

You can by the way combine RegEx like this (.*)GUIDE_MIB(.*)|(.*)GUIDE_(.*)P4 the pipe "|" works well one positive matches. negative matches don't make fun with it


Am Freitag, 19. September 2014 09:38:17 UTC+2 schrieb Jan Seidel:

Gunnar Strand

unread,
Sep 19, 2014, 6:04:24 AM9/19/14
to jenkins...@googlegroups.com

On 09/19/14 11:27, Jan Seidel wrote:
True (.*) is very greedy, but (?!P4) is a but not P4 and I would expect it to work.
It isn't as I just figured out in a test run with a completely different RegEx to skim for other jobs :/

I don't know what kind of regex the Matrix supports, but "X(?!Y)" normally means "X" not followed by "Y". If you precede it with a match anything repeating pattern: "X.*(?!Y), the parser will happily match "XY" where "Y" is matched using the ".*" pattern, and determine that the matching string XY is not followed by "Y", hence it returns true. Using a negative look-ahead pattern is tricky, you must anchor the pattern to something.

BR
Gunnar


I got to tweak it and create a set of project rules and then combine them for each user depending on what they need to know *geesh*
Thanks for the hint.

Jan


Am Freitag, 19. September 2014 09:38:17 UTC+2 schrieb Jan Seidel:
Hi all,

I have a weird issue with a regular expression.
Hopefully you can help me out here.

I have a RegEx like (.*)GUIDE_(?!MIB)(.*)(?!P4)
It should, as far as I can tell, list all jobs containing GUIDE but exclude GUIDE jobs with a MIB in the middle or P4 at the end of the project name, right?

My problem here is, that jobs with a P4 at the end still are listed :(

I have for testing purposes tried all kind of RegEx that crossed my mind but without any success.
Later I tried just to exclude job names with a P4 at the end. No joy

The RegEx I used was: (.*)GUIDE_(.*)(?!P4)
The list is then completely emtpy.

Can someone tell me where I glitch?

Cheers
Jan
--

Gunnar Strand

unread,
Sep 19, 2014, 6:20:49 AM9/19/14
to jenkins...@googlegroups.com

On 09/19/14 11:44, Jan Seidel wrote:
Yay, got it.
A set of projects rules. each one containing a positve match and then combine the rule works well.

One odd thing I learned is, that you don't need wildcards if your match has a trailing string.

e.g. MB_GUIDE_Pxxxxxxx_P4_DELIVERY will also be found when you work with (.*)GUIDE_(.*)P4

You can by the way combine RegEx like this (.*)GUIDE_MIB(.*)|(.*)GUIDE_(.*)P4 the pipe "|" works well one positive matches. negative matches don't make fun with it

The "^" and the "$" commands matches the beginning of the string and the end of the string, respectively. Some parsers, or rather some designers, adds these commands to the pattern, ie "GUIDE_.*P4" would actually be "^GUIDE_.*P4$" and would not match "MB_GUIDE_P4" nor "GUIDE_P4_DELIVERY".

However, that does *not* appear to the case above, which is why your pattern matches the "P4_DELIVERY" name above. You need to add "^" and "$" yourself if you want to include "starts with" and "ends with" parts for the pattern.

If you know which text the '.*' will match, then you could do this:

GUIDE_(?!MIB)(FOO|BAR|BAZ)(?!P4)$

This should match any name containing GUIDE_, where GUIDE_ is not followed by MIB, and then followed by one of a specific list of possible strings, which are not followed by "P4". Remove the "$" at the end if the name may contain other text after the FOO/BAR/BAZ part.

BR
Gunnar


Am Freitag, 19. September 2014 09:38:17 UTC+2 schrieb Jan Seidel:
Hi all,

I have a weird issue with a regular expression.
Hopefully you can help me out here.

I have a RegEx like (.*)GUIDE_(?!MIB)(.*)(?!P4)
It should, as far as I can tell, list all jobs containing GUIDE but exclude GUIDE jobs with a MIB in the middle or P4 at the end of the project name, right?

My problem here is, that jobs with a P4 at the end still are listed :(

I have for testing purposes tried all kind of RegEx that crossed my mind but without any success.
Later I tried just to exclude job names with a P4 at the end. No joy

The RegEx I used was: (.*)GUIDE_(.*)(?!P4)
The list is then completely emtpy.

Can someone tell me where I glitch?

Cheers
Jan
--

Richard Lavoie

unread,
Sep 19, 2014, 6:53:12 AM9/19/14
to jenkins...@googlegroups.com
As a few people already mentioned you need to anchor your pattern. Otherwise, .* might match what you want to exclude and attempt the match after it, resulting in an unwanted result.

What your regx is doing in words is this:

(.*) match anywhere
GUIDE_ : normal text match
(?!MIB) : immediately not followed by MIB
(.*) : match anywhere
(?!P4) : not followed by P4

Now, what you want to do if MIB and P4 can be anywhere after GUIDE_ is to match every character and the make sure it is not followed by any of those, up to the end. The regex for that would be:

(.*)GUIDE_(?:(?!MIB)(?!P4).)*$

The fancy part at the end translates into :

Make sure the following text is neither MIB or P4 when you try to match the next char, up to the end of string. it does not make a distinction weither MIB comes before or after P4. It will make sure any of those is not after GUIDE_

Richard Lavoie
--

Richard Lavoie

unread,
Sep 19, 2014, 6:57:18 AM9/19/14
to jenkins...@googlegroups.com
If you want to allow that MIB can appear anywhere else other that immmediately after GUIDE_ you can change the regex for :

(.*)GUIDE_(?!MIB)((?!P4).)*$

Richard Lavoie

Jan Seidel

unread,
Sep 22, 2014, 4:28:48 AM9/22/14
to jenkins...@googlegroups.com, gunnar...@ericsson.com
X(?!Y) works when it is used in a RegEx but the plugins botches as soon as you start to combine several negations.

X(?!Y)|A(!?B) won't behave as expected.
Applying the de Morgan's law (double negation to create and AND from an OR) does not interest the plugin at all.
I tried (!?(X(?!Y)|A(!?B)), (!?((X(?!Y)|A(!?B))) and all other combinations that made sense to me.

Latest crime I committed was: (!?((!?((.*)GUIDE_MIB.*))|(!?((.*)GUIDE_Pxxxxxx(.*)))|(!?((.*)GUIDE_(.*)P4))))
Looks like that Jenkins does not care about negations when combining patterns :/

Jan Seidel

unread,
Sep 22, 2014, 4:31:51 AM9/22/14
to jenkins...@googlegroups.com, gunnar...@ericsson.com
GUIDE_(?!MIB)(FOO|BAR|BAZ)(?!P4)$ is a nice idea.
I have been working with it quite at the beginning of my test cases but I neglected ^ and $.
This may be the hint I needed to anchor the parttern. :)

Cheers

Jan Seidel

unread,
Sep 22, 2014, 4:37:35 AM9/22/14
to jenkins...@googlegroups.com
Nice advice! :)

what does ?: actually imply?
Where do I find this information?


If you want to allow that MIB can appear anywhere else other that immmediately after GUIDE_ you can change the regex for :
(.*)GUIDE_(?!MIB)((?!P4).)*$

This pattern confuses me a bit... Ooooohh.... now I get it. Dumb little me XD
That explains another issue I have. *geesh* I start to become dizzy of RegEx

Thanks for helping out.
I will do some tests right away

Cheers
Jan

Jan Seidel

unread,
Sep 22, 2014, 4:40:14 AM9/22/14
to jenkins...@googlegroups.com
It works like a charm :)

Thank you two for helping me out on this one.

Jan

Am Freitag, 19. September 2014 09:38:17 UTC+2 schrieb Jan Seidel:

Jan Seidel

unread,
Sep 22, 2014, 5:03:05 AM9/22/14
to jenkins...@googlegroups.com
Last but not least.
Looks like Jenkins adheres to Java Regex, even if I sometimes doubted it.

"(?:X) X, as a non-capturing group"
Does this mean ... "don't jump on this pattern"?
Sorry, I'm no developer :)

Richard Lavoie

unread,
Sep 22, 2014, 6:04:36 AM9/22/14
to jenkins...@googlegroups.com
What ( ) does is that whenever it matches the inner pattern, it keeps it in memory for further reference in code.

(?: ) allows to have a complex inner pattern with OR cases without keeping the matched content in memory.

That's what mean "non-capturing" for (?:).

Also www.regular-expressions.info has a really nice reference on regex and also tells you which engine supports which feature.

Richard Lavoie
--

Jan Seidel

unread,
Sep 22, 2014, 7:15:13 AM9/22/14
to jenkins...@googlegroups.com
Thanks for the explanation :)
The site you have mentioned is indeed a real nice one. It is already added to my bookmarks.

Cheers
Jan

Raj Kiran Neerukonda

unread,
Jul 10, 2017, 3:24:07 AM7/10/17
to Jenkins Users
Hello,

I am having similar problem. Right now i am having multiple directories, i need to control access to sub directories. For example, i have component specific jenkins jobs in 

parent/project/component/* 

How can i configure this pattern in role base authentication?

I tried giving parent/project/component|parent/project/component/* 

it dint worked. Please help me. 

Thank you,

Raj Kiran.

Prachi Khadke

unread,
Jul 12, 2017, 1:12:22 PM7/12/17
to Jenkins Users
Hi Raj Kiran

Did you find a solution to your problem? I have the same issue. Need to figure out the correct regex for project level role based access control. 

Regards
Prachi

Prachi Khadke

unread,
Jul 12, 2017, 3:01:18 PM7/12/17
to Jenkins Users

Artur Szostak

unread,
Jul 13, 2017, 3:12:39 AM7/13/17
to Jenkins Users
Looks like you might be confusing a glob with a regex.
You probably want:
(parent/project/component)|(parent/project/component/.*)

________________________________________
From: jenkins...@googlegroups.com <jenkins...@googlegroups.com> on behalf of Raj Kiran Neerukonda <rajki...@gmail.com>
Sent: 10 July 2017 01:11:30
To: Jenkins Users
Subject: Re: Issue with RegEx for project roles in Matrix Authorization Strategy Plugin
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com<mailto:jenkinsci-use...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b2514fd7-d0d7-4e8e-97e3-58e4099eadf2%40googlegroups.com<https://groups.google.com/d/msgid/jenkinsci-users/b2514fd7-d0d7-4e8e-97e3-58e4099eadf2%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages