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

Remove a SINGLE instance of a substring from within a string (smart values)

Chris Smith February 13, 2023

Hi, 

    I am trying to remove a single instance of the string REM (which is a smart value) from the string TOT (which is also a smart value). Im within a branch when doing this but I dont think that the branch is the problem. So basically I want to do something like

 

Assign TOT.remove(REM) to TOT

If TOT is ABCDEFG and REM is CDE, then this works fine

But if TOT is ABCDEFGABCDEFG and REM is CDE, then I want it to only remove the first occurrence. But of course thats not the syntax of remove.

So I tried things like getting the left and right hand sides separately and then concatting

so left would be TOT.left(TOT.indexOf(REM))

and right would be TOT.right(TOT.indexOf(REM)+REM.length()) 

and so the whole thing would be 

TOT.left(TOT.indexOf(REM)).concat(TOT.right(TOT.indexOf(REM)+REM.length()))

Except this means nesting smart values, and JIRA doesnt like that (at least thats what I understand).

So does anybody have a cunning way of doing this?

 

3 answers

0 votes
Chris Smith February 15, 2023

So I think I managed to get something working with doing result.concat(str.substringBefore(toBeReplaced).concat(str.substringAfter(toBeReplaced) 

Anybody see a hole in the logic?

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.
February 15, 2023

Ah, yeah, that will work because substringAfter is matching the first occurrence of your string.

Nice one!

Like Chris Smith likes this
0 votes
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.
February 13, 2023

Hi @Chris Smith

As @David Pezet pointed out, this can be done with replaceAll and the judicious use of regular expressions. So this worked for me:

{{TOT.replaceAll(REM.concat("(.+)"),"$1")}}

What it's doing is replacing REM and everything that comes after it, which is represented as (.+).

We are replacing that with the "captured" text (which is what the parentheses are for).

So then, in the string ABCDEFGABCDEFG we are replacing:

CDE(FGABCDEFG)

with

FGABCDEFG

Hopefully that's clear.

P.S. @David Pezet I am very interested in what undocumented Java functions you've found. removeFirst did not work for me.

David Pezet February 13, 2023

@Darryl Lee Unfortunately I don't immediately recall, and don't have a list. This was a while back. The page seems a lot more complete than I remember, so it is possible it is now up to date (or I could be mis-remembering). If I happen across any, I'll try to remember to update here.

Chris Smith February 15, 2023

Hi Darryl, Thanks for the tip - so is this regex just replacing the first occurrence (which is what I wanted), or all occurrences?

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.
March 25, 2023

Oof, was going through my old answers, and realized I didn't reply @Chris Smith , sorry - yes, this replaces only the first occurrence of REM.

Even though the function is called replaceAll, what we are replacing is CDE+"everything that follows it" (that's what the "(.+)" is about) with just everything that follows it (that's what the "$1" is about).

There's only one instance of CDE+"everything that follows it", so in effect it's only doing one replacement.

Regex is weird. I know. I tested this here: https://regex101.com/

It's a super-useful site:

Screen Shot 2023-03-25 at 11.24.26 PM.png

Like Chris Smith likes this
0 votes
David Pezet February 13, 2023

You can try using String.replacefirst() (I have not tried this myself) or you can try a regular expression that accomplishes what you are looking with String.replaceAll() (which I have used). There are lots of online tools to help figure out the regex (e.g., regex101).  Hope this helps.

Chris Smith February 13, 2023

Hi David - thanks for the reply. I dont see a replaceFirst in the JIRA reference? https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/

David Pezet February 13, 2023

Right. I've had some luck using other Java functions that aren't expressly listed, so worth a shot to try if it makes it any easier for you. Otherwise, you may need to figure out a regular expression that only returns the first instance maybe through greedy/ungreedy options or groupings. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events