How to work with substrings?
Asked by Niklas Malmberg:

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](https://community.atlassian.com/t5/image/serverpage/image-id/330842i9A64FCDC752F8EB4/image-size/large?v=v2&px=999 "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](https://community.atlassian.com/t5/image/serverpage/image-id/330843i5D832B35EB226ECC/image-size/large?v=v2&px=999 "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](https://community.atlassian.com/t5/image/serverpage/image-id/330844iF2891F2187B94762/image-size/large?v=v2&px=999 "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.


Answers:
author: Darryl Lee
body: Aw c'mon guys, a bunch of IF-ELSE statements isn't any fun!

I think [@Niklas Malmberg](/t5/user/viewprofilepage/user-id/4821791) 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](/t5/user/viewprofilepage/user-id/3131564) made a [video](https://www.youtube.com/watch?v=4pnUWAY-dMQ) about it this.)

#### But well, this is the fun part... {#toc-hId-346715579}

It seems that what you really want is the last number before the closing parentheses. So you can use the [match()](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#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>

comments: author: Darryl Lee
body: 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](https://community.atlassian.com/t5/image/serverpage/image-id/330996i9E4236681EC1A273/image-size/large?v=v2&px=999 "Screenshot 2024-06-20 at 10.34.37 PM.png")


author: Jovin
body: [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) - 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!


author: Niklas Malmberg
body: 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](https://community.atlassian.com/t5/image/serverpage/image-id/332742i4F826A7826F21D6B/image-size/large?v=v2&px=999 "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](/t5/user/viewprofilepage/user-id/1365322) !


author: Niklas Malmberg
body: 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)"


author: Darryl Lee
body: 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>


author: Ste Wright
body: Hi [@Niklas Malmberg](/t5/user/viewprofilepage/user-id/4821791)

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

comments: author: Niklas Malmberg
body: 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?


author: Ste Wright
body: 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


author: Niklas Malmberg
body: 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?


author: Ste Wright
body: Hi [@Niklas Malmberg](/t5/user/viewprofilepage/user-id/4821791)

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


author: Jovin
body: 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 :)

comments: author: Niklas Malmberg
body: Hi [@Jovin](/t5/user/viewprofilepage/user-id/5104734) , 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.


author: Jovin
body: Hi [@Niklas Malmberg](/t5/user/viewprofilepage/user-id/4821791),

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](https://community.atlassian.com/t5/image/serverpage/image-id/330863i854DA0118018E2E5/image-size/large?v=v2&px=999 "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 ....


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


author: Niklas Malmberg
body: Right, that worked.  
I had to add a block of ELSE IF for each possible value, like   

![Screenshot 2024-06-20_000191.png](https://community.atlassian.com/t5/image/serverpage/image-id/330885i4645425052475F4F/image-size/large?v=v2&px=999 "Screenshot 2024-06-20_000191.png")![Screenshot 2024-06-20_000192.png](https://community.atlassian.com/t5/image/serverpage/image-id/330886i80DBCB3A99BEAEDD/image-size/large?v=v2&px=999 "Screenshot 2024-06-20_000192.png")

so it's not elegant but it did it's work just fine.   
Thanks [@Jovin](/t5/user/viewprofilepage/user-id/5104734)


author: Jovin
body: 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!