How to remove content within round brackets in a string in Velocity? | Community
Skip to main content
Cristina_Leonet
Level 2
May 16, 2019
Question

How to remove content within round brackets in a string in Velocity?

  • May 16, 2019
  • 4 replies
  • 4850 views

We want to have FirstName LastName (Abbr.) displayed as FirstName LastName.

I'm trying with regex but is not working, I'm new in velocity, and I would like kindly ask what's wrong in the script:

I tried this version:

#set ($string = ${lead.Account_Owner__c})
#set ($var1 = $string.replace("/\(\w+\)/", ""))
This is the Account Owner Without Abbreviation: $var1

This is not removing anything.

-----

This the other version I tried:

#set ($string = ${lead.Account_Owner__c})
#set ($var1 = $string.replace(/\(\w+\)/, ""))
This is the Account Owner Without Abbreviation: $var1

This script gives an error.

Thanks in advance!

Cheers,

Cristina

#velocity my token#velocity script #velocity error #velocity scripting

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

SanfordWhiteman
Level 10
May 16, 2019

Please highlight your code using the Syntax Highlighter (use Java as it's closest to VTL) then we'll continue.

Cristina_Leonet
Level 2
May 16, 2019

Hello Sanford! Thanks a lot for your reply!

Do you think we can continue now?


#set ($string = ${lead.Account_Owner__c})#set ($var1 = $string.replace("/\(\w+\)/", ""))This is the Account Owner Without Abbreviation: $var1

#set ($string = ${lead.Account_Owner__c})#set ($var1 = $string.replace(/\(\w+\)/, ""))This is the Account Owner Without Abbreviation: $var1
SanfordWhiteman
Level 10
May 16, 2019

You're overly restricting the characters in parens. You want to strip anything in parentheses that's anchored at the end of the string, including leading and trailing whitespace.

#set ($accountOwner = $lead.Account_Owner__c)
#set ($accountOwnerSimple = $accountOwner.replaceAll("\s*\(.*\)\s*$", ""))
This is the Account Owner Without Abbreviation: $accountOwnerSimple

Also:

  • always use informative variable names so your code is maintainable
  • don't use curly braces (formal references) except in output
Cristina_Leonet
Level 2
May 16, 2019

Thank you so much for guides! I need to do more training on velocity script, and I'm happy to learn from the bests!