Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I use a regex or other pattern match in a Confluence Velocity Macro Template

Hugh Kelley
Contributor
March 2, 2023

I want to use a regex to test a string in a macro #if.

I can't find a string method that will test a regex.

#if($labelName.match('.*'))
<a href="/label/$spaceKey/$label">$label</a>
#end

1 answer

0 votes
Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 3, 2023

Hi @Hugh Kelley 

Could not test it, but java gives the Matcher Class. Can you try with this?

#set($regex = ".*")
#set($matcher = $regex.matcher($labelName))
#if($matcher.matches())
<a href="/label/$spaceKey/$label">$label</a>
#end

Regards, Dominic

Hugh Kelley
Contributor
March 3, 2023

@Dominic Lagger ,  thank you for the hint.  I had tried something similar initially but couldn't get the .matches() method to work.   When I saw your suggestion, I dug a little deeper and realized that I wasn't getting the .matches() method because my variable was of class com.atlassian.confluence.labels.Label,  not a Java String.

Once I added a .getName() method, I got a String and things worked as hoped.

#foreach ( $labelling in $page.getLabellings() )
#set( $label = $labelling.getLabel().getName())
#if( $label.matches($paramFeaturedLabels) )
<a href="/label/$spaceKey/$label">$label</a>
#end
#end
 
Like Dominic Lagger likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events