How to do a split when the delimiter is a period / dot?

I’m trying to use Velocity to take a string that is delimited by a dot / period and remove the last delimited piece from the text. Attempts to use built in operations seem to fail even with the double back slashes.

I found some Velocity logic that may work, but I’m not sure how to write this in the transform:

#set($delimitedText = "This.is.a.delimited.text")

#set($splitText = $delimitedText.split("\\."))

#set($size = $splitText.size())

#set($value = $splitText.get(0) + #foreach($i in [1..$size-3])"."$splitText.get($i)#end + $splitText.get($size-2))

I try to set the value key to:

"value": "#set($slpitText = $pj.split("\."))#set($size = $splitText.size())#set(newText = $splitText.get(0))#foreach($i in [1..$size-3])#set($newText = "$newText.$splitText.get($1)")#end#$newText$splitText.get($size-2)" 

When I try to save this in vscode I receive the error: “The request could not be parsed.”

Give this a try
#set($splitText = $delimitedText.split("[.]"))

Same error.

Changed the value key to:

"value": "#set($slpitText = $pj.split("[.]"))#set($size = $splitText.size())#set(newText = $splitText.get(0))#foreach($i in [1..$size-3])#set($newText = "$newText.$splitText.get($1)")#end#$newText$splitText.get($size-2)" 

Hello @ts_fpatterson,

You can accomplish this with out of the box transform operations. Your requirements are similar to that of this post.

The first part of this transform will take your input and find the last index of . to use as the integer to begin the substring from.

The second piece beginOffset is needed as the last index of . will include .test and will trim the next 1 character.

Finally, input your . seperated string to the substring transform with begin and beginOffset.

{
	"attributes": {
		"begin": {
			"attributes": {
				"input": {
					"attributes": {
						"value": "this.is.a.test"
					},
					"type": "static"
				},
				"substring": "."
			},
			"type": "lastIndexOf"
		},
		"beginOffset": 1,
		"input": {
			"attributes": {
				"value": "this.is.a.test"
			},
			"type": "static"
		}
	},
	"type": "substring"
}

In your original post I noticed this…
On the top section you have mentioned \\ in regex
#set($splitText = $delimitedText.split("\\."))
However in the string you have single \
"#set($slpitText = $pj.split("\."))#set(.....
Would this be the reason for error?

Once I created it through postman and I included the brackets, this got rid of the errors, but the evaluation is still returning the full string.

I’m still trying to understand the nested structure.

Do you by chance have an example of a full transform so I can see the full structure?

Example of what I’m doing with the velocity isn’t working. It displays the same data as what is stored on the source. I would like to see a velocity solution to this too.

{
    "id": "71b49db6-9bd8-451e-9d0d-25d1bffffffff",
    "name": "Job Split",
    "type": "trim",
    "attributes": {
        "requiresPeriodicRefresh": "true",
        "pj": {
            "attributes": {
                "attributeName": "PJ",
                "sourceName": "TST"
            },
            "type": "accountAttribute"
        },
        "value": "(#set($lastDotIndex = $pj.lastIndexOf('.'))#if($lastDotIndex > 0)#set($outputString = $pj.substring(0, $lastDotIndex))#else#set($outputString = $pj)#end)$outputString"
    },
    "internal": false
}

@ts_fpatterson,

That is actually the full code required to accomplish the task, adding in your input it would look like below:

{
	"name": "Job Split",
	"type": "substring",
	"attributes": {
		"begin": {
			"attributes": {
				"input": {
					"attributes": {
						"attributeName": "PJ",
						"sourceName": "TST"
					},
					"type": "accountAttribute"
				},
				"substring": "."
			},
			"type": "lastIndexOf"
		},
		"beginOffset": 1,
		"input": {
			"attributes": {
				"attributeName": "PJ",
				"sourceName": "TST"
			},
			"type": "accountAttribute"
		}
	}
}

With just velocity, it would look like this.

{
	"name": "Job Split",
	"type": "static",
	"attributes": {
		"pj": {
			"attributes": {
				"attributeName": "PJ",
				"sourceName": "TST"
			},
			"type": "accountAttribute"
		},
		"value": "#set($int = $pj.lastIndexOf('.') + 1)$pj.substring($int,$pj.length())"
	}
}

I think Fred is trying to get the characters before last . whereas the transform you have provided returns the characters following last .

Whoops, I think you are right. I read the first post a little closer and saw “remove the characters after the last .

A few small changes to each then!

OOB Operations

{
	"name": "Job Split",
	"type": "substring",
	"attributes": {
		"end": {
			"attributes": {
				"input": {
					"attributes": {
						"attributeName": "PJ",
						"sourceName": "TST"
					},
					"type": "accountAttribute"
				},
				"substring": "."
			},
			"type": "lastIndexOf"
		},
		"begin": 0,
		"input": {
			"attributes": {
				"attributeName": "PJ",
				"sourceName": "TST"
			},
			"type": "accountAttribute"
		}
	}
}

Velocity

{
	"name": "Job Split",
	"type": "static",
	"attributes": {
		"pj": {
			"attributes": {
				"attributeName": "PJ",
				"sourceName": "TST"
			},
			"type": "accountAttribute"
		},
		"value": "#set($int = $pj.lastIndexOf('.'))#if($int != -1)$pj.substring(0,$int)#else$pj#end"
	}
}
2 Likes

Thank you!! This is helping a lot in understanding the structure.

Hello Fred!

Just an observation using the velocity, in the “value”, if you used to use double quotes inside the substring, lastIndexOf, or other operations, it’s needed to use a back slash \ before the double quotes to scape them, it will do the value understand that the double quotes that you’re using is for the operation and not to close the value

example using your velocity:

"value": "#set($slpitText = $pj.split(\".\"))#set($size = $splitText.size())#set(newText = $splitText.get(0))#foreach($i in [1..$size-3])#set($newText = \"$newText.$splitText.get($1)\")#end#$newText$splitText.get($size-2)"```
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.