Hello dear Atlassian-Community,
I am currently trying to build something that would make an old automation of mine way shorter and incredibly more efficient, but the whole idea hinges on "being able to create a RegEx dynamically and handing it over to another function"
Here is the functional part of my concept:
0) Trigger: Doesn't matter for this case
1) Action: Create Variable
This is a comma separated gathering of calculations done beforehand. In the real world application this will be provided by a lookupObjects-List-Iteration and (hopefully clever) replace-functions.
Name: myVarInput
Value: 1~A, 111~B, 1~C, 11~D
Worth noting: "More 1 = more important" I would want to get "B" back in some way and i thought of trying with regEx.
2) Action: Create Variable
Name: maxOnesMinusOne
Value: 2
We will not talk about how I will get the max size / maximum numbers of "1"s from {{myVarInput}} here and subtract 1 - lets just assume I get the Value 2 somehow ;)
EDIT: I have found and shared a way to extract this value in the comments. I still feel it remains out of scope here but for anyone there now exists two-create-variable-steps solution that doesn't even require any RegEx to work. Crazy, I know.
3) Action: Create Variable
Name: myDynamicBlock
Value: [1]\{1,{{maxOnesMinusOne}}\}
Note: I would want to have the output [1]{1,2} for our example input
4) Action: Create Variable
Name: regExTest
Value: {{myVarInput.replace(myDynamicBlock,"0")}}
Note: Following our example line, I would want to have the output 0~A, 01~B, 0~C, 0~D here.
5) Create Variable Action with Further refinement (out of scope for this question, because it only requires a "fixed" RegEx at this point):
Next step would be {{regExTest.split(", ").remove("0~.*")}} (RegEx test pending) to have only the most important (multiple possible) entries left.
Questions:
1) Is the handover from maxSizeMinusOne to myDynamicBlock possible (can i exclude curly brackets somehow)?
2) Is the handover from myDynamicBlock to regExTest possible? (how do i get it to execute properly)
Edit/carifications:
- {{myVarInput}} is vastly reduced in complexity and simply gauging the size of the individual chunks separated by commas will not work in the real world application.
- I have corrected some spelling/consistency mistakes after posting
- Upon some further investigation, I have found this post with an answer related to my musings from @Bill Sheboy - Solved: Jira automation - using variable in another variab... maybe there is indeed some hope for me?
- the 4th action initially and wrongs used {{myVar.replace[...] instead of {{myVarInput.replace[...] - i have adjusted this to avoid confusing anyone reading or trying to reproduce this in the future. Thanks to everyone for noticing!
Kind regards,
Ludwig
Hi @Ludwig Einicke ,
Yes, both handovers are possible. The main corrections are to build the complete expression in its own variable and use replaceAll(), because replace() treats its first parameter as literal text.
With maxOnesMinusOne = 2, the first variable renders as (?:^|(?<=, ))1{1,2}, and the second returns 0~A, 01~B, 0~C, 0~D. The boundary limits the replacement to the start of each comma-separated entry, preventing the final 1 in 111 from being replaced a second time. Also note that the source variable is myVarInput, not myVar.
Your final remove("0~.*") would also be a literal operation, not a regex filter. For the example above, {{regExTest.match("(?:^|, )01+~([^,]+)")}} returns B; if several entries have the same maximum, match() returns them as a collection.
Add a Log action after each variable while testing so you can verify the fully rendered pattern and result. Atlassian documents the Create variable action and the differences between replace(), replaceAll(), and match().
Thank you so much! This was exactly the compact and "improving my general knowledge" type of advice I needed. I managed successfully test my implementation!
Kind regards,
Ludwig
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oh - and for anyone wondering - here is how MaxOnesMinusOne gets created (I had to split it into two actions create Variable and I for sure wouldn't call it pretty, but it does the job):
2.1) Action: Create Variable
Name: maxOnes
Value: {{myVarInput.split(",").substringBefore("~").asNumber.max.format}}
Output for the original example: 111
2.2) Action: Create Variable
Name: maxOnesMinusOne
Value: {{MaxOnes.length.minus(1)}}
Ourput for the original example: 2
The split into two seperate "Create Variable" Actions is the workaround for the fact that I need the max() function to get the highest number, but having my string concerted to an integer leaves me unable to calculate "length" without re-converting it to a string by reading it into another variable.
There is no asSting/toString function as far as i am aware of and as far as this thread is concerned: Number to string automation
At least i found format() on the way, meaning the initial 111.0 resulting from the conversion during asNumber() gets converted to a neat 111 without me having to do further replaces/matches/etc.
Kind regards,
Ludwig
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ludwig,
The "Parameters not closed" error is the standard trap — you can't nest a {{...}} block inside another {{...}}block's arguments. The fix, confirmed in this solved thread, is to reference a previously-created variable bare, with no double braces, as a function argument — that's exactly the accepted answer's pattern: {{project.versions.name.match(varRegEx)}}, where varRegEx is passed unwrapped.
On your two questions:
{{ }} sequence, a single { or } is just literal text to it. So: [1]{1,{{maxOnesMinusOne}}} resolves cleanly to [1]{1,2}. The backslash-escaped version you tried (\{1,2\}) would actually break the regex — \{ means a literal brace character in most regex engines, not a quantifier delimiter.{{myVarInput.replace(myDynamicBlock,"0")}} (double-check myVar vs myVarInput, looks like a typo). Referencing myDynamicBlock bare is the same pattern as the accepted answer above. Separately: .replace()only replaces the first match — given your comma-separated-list example, you probably want .replaceAll().If it's still not resolving, log {{myDynamicBlock}} on its own line right before the failing step to confirm it's actually producing [1]{1,2} rather than something the quantifier braces mangled.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you and good catch on the myVar vs. myVarInput! I lacked some general RegEx-knowledge, but Germáns comment below compensated for my shortcomings there. I could successfully test the implementation now.
Kind regards,
Ludwig
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.