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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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

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.
Feb 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.
Feb 13, 2023 • edited

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.

@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.

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.
Mar 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

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.

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/

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