Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to work with substrings?

Niklas Malmberg
Contributor
June 20, 2024

I have a field for estimations, "T-shirt size", where the developers can estimate the amount of work before they actually start. This is a text field, with these options right now:
 Screenshot 2024-06-20_000189.png

As a goal, I want to extract the larger numeric value for each entry and use that for later calculation in automation rules.

I found several substring-functions and started testing with them to figure out how to parse the string, but so far these returns nothing for me :(

I have a manually triggered test rule that simply logs output for now: 
Screenshot 2024-06-20_000190.png

Now, running this on a subtask with T-shirt size field set to "M (3-5)" I get this output in audit log:
Screenshot 2024-06-20_000188.png

What am I doing wrong? 

I'm running a Team-managed Jira software project in Cloud

 

As final result, I want to extract the "5" and convert it to a number, but neither of my substring seems to do anything useful.

3 answers

3 accepted

1 vote
Answer accepted
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Aw c'mon guys, a bunch of IF-ELSE statements isn't any fun! 

I think @Niklas Malmberg the problem is that you should be using:

{{issue.t-shirt size.value}}

I don't know if Atlassian ever documented the value thing. There's a bit about it buried here:

https://support.atlassian.com/cloud-automation/docs/find-the-smart-value-for-a-field/

(Oh neat, @Fabian Lim made a video about it this.)

But well, this is the fun part...

It seems that what you really want is the last number before the closing parentheses. So you can use the match() operator for that.

So what you could do is:

{{issue.t-shirt size.value.match("\D+(\d+)\)")}}

That's a regular expression, and what it means is:

\D = a non-digit character

+ = at least one or more of the previous characters 

\d = a digit-character

+ = again, at least one or more of the previous characters

The digits are "captured" in parentheses: (\d+)

And lastly, the key is that we're looking for that closing parentheses in the select value, which we have to escape with a backslash, so \)

An incredible tool for better explaining regular expressions and TESTING them is regex101.com, and I've saved the one above for you to check out:

https://regex101.com/r/zI48rf/1

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Here's of a screenshot of the rule in action. Or well, logs of it:

Screenshot 2024-06-20 at 10.34.37 PM.png

Jovin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 21, 2024

@Darryl Lee - Awesome response, and quite elegant in the use of complex operators in the automation. I prefer to keep things more readable (as I do with my programming too). It may be more verbose or a few extra "lines of code", but I find it easier to debug 😂

Always love seeing the more complex and elegant things though!

Niklas Malmberg
Contributor
July 1, 2024

Now we're talking !

1. Yes,
.value was the lacking part. Now my substring functions return something, and logical too.
The original example above with input "M (3-5)" yielded these outputs: 

Screenshot 2024-07-01_000206.png
Makes complete sense. 

2. To use RegExps instead did not even enter my mind, but it's elegant and a much better solution than mucking with substrings. Excellent, Sir! 

3.
Yes, I agree with Jovin too, that the resulting rule might not be very easily readable (I miss the possibility of adding comments anywhere I want in a Jira automation rule).
Still, the rule description field is available, and most rules are after all quite short.  

Thank you @Darryl Lee ! 

Niklas Malmberg
Contributor
July 1, 2024

BTW, the RegExp suggested did not handle the string "XS (0,5)" properly. 

It's a rather local standard to express decimal numbers with a comma, I know a point/dot separator is more common.

But anyhow, a non-digit character made the RegExp "\D+ (\d+)\)" return wrong, 5 instead of 0,5 
I played around on regext101.com and landed in regExp "([0-9.]+)\)", and using decimal dot in all my strings like "XS (0.5)"

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2024

Oh! I totally missed that you were using fractional points! 

So yeah, I think you could keep using a comma, if you used:

([0-9,]+)\)

Confirmed in regex101 that this works: https://regex101.com/r/1L6pBV/1

1 vote
Answer accepted
Ste Wright
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Hi @Niklas Malmberg 

It feels like you're making this more complex than it needs to be.

For example, why does it need to be a text field? Why not a select list or a number field to make this simpler?

Ste

Niklas Malmberg
Contributor
June 20, 2024

This field is not really mine to edit, it's used by several projects at the company. And since it's call T-shirt size, The XS, S, M, L-notation is rather expected. 

But how come the substring functions all returns nothing?

Ste Wright
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Not sure, but just want to ensure they're necessary.

Is it that you will take whatever number is after the "-" each time? Or, you just want the biggest number every time? EG. for small you always want 2?

Could you also clarify what you'll use the numbers for?

Ste

Niklas Malmberg
Contributor
June 20, 2024

The question is really:
For a string like "M (3-5)" residing in a field called 't-shirt size' on a Jira issue, how can I extract the last numerical character(s), so I can convert that to a numerical value later on? 

According to the documentation, given input "M (3-5)", the function {{ issue.t-shirt size.substringBetween("-",")") }} ought to return the string "5", but for some reason it doesn't. 

Does anyone know how the parsing should be written to accomplish that? 

Ste Wright
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Hi @Niklas Malmberg 

The reason I'm asking is IF/ELSE statements could be easier here - i.e

  • IF value = MINI
  • THEN create a variable of 0
  • ELSE-IF value = S (1-2)
  • THEN create a variable of 2
  • ...etc

---

For the smart values though, I tested this in a test environment and it works fine for me.

One thing you could try is using the exact field name, including the capitalisation of "T-Shirt Size". This can be an issue sometimes!

Ste

 

0 votes
Answer accepted
Jovin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Hey Niklas,

For simplicity have you tried adding an "IF ELSE" condition and simply iterating over the options? Meanwhile this is less "dynamic" it's guaranteed to work unless you change the options :)

Niklas Malmberg
Contributor
June 20, 2024

Hi @Jovin , sure I will need to add conditions, but for now I started simple - I just want to figure out how to parse a string and extract a part that I later could turn into a number.

So my test is just this, first displaying the string to audit log and then try to use several ways to look into subparts of the string value.
As you can see, I'm 1) searching for the divider - between number chars, 2) searching for the ending paranthesis, and also 3) trying to look for both. 

Unless I can find any number in the string, subsequent IF-ELSE will not do much. 

Jovin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Hi @Niklas Malmberg,

I can see that the T-Shirt size is a select list, in that case you simply need to make the IF statement be comparing the "Issue Field", like the screenshot below, simply replace Change reason with your T-shirt size and for each of the IF/ELSE/ELSE/ELSE choose a different option.

By using this method you don't need to use substrings at all.

Screenshot 2024-06-20 123848.png

Following the condition, you can then do an Action to "Edit Issue" with the field desired to capture the scoring you've got.

e.g.

IF "T-shirt size" EQUALS "Small (0,5)"

EDIT ISSUE FIELD "Story Points" with value 0,5

ELSE IF ....

Niklas Malmberg
Contributor
June 20, 2024

Yes, that might work, will try that for sure. 
Just annoyed that my substring*-calls are just not working at all :/

Niklas Malmberg
Contributor
June 20, 2024

Right, that worked.
I had to add a block of ELSE IF for each possible value, like 

Screenshot 2024-06-20_000191.pngScreenshot 2024-06-20_000192.png

so it's not elegant but it did it's work just fine. 
Thanks @Jovin 

Like Jovin likes this
Jovin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2024

Amazing! I sometimes get caught in a cycle of making things more complex too, community is an amazing resource for sense-checking and simplifying and I'm glad I could be a part of that journey!

Like Niklas Malmberg likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events