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?
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?
Ah, yeah, that will work because substringAfter is matching the first occurrence of your string.
Nice one!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Darryl, Thanks for the tip - so is this regex just replacing the first occurrence (which is what I wanted), or all occurrences?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.