regex in velocity template

Jan Post August 24, 2011

I'm trying to accomplish a regex test in velocity. However, I can't get to work. What am I doing wrong?

My code:

#set( $str = "$issue.reporter")

#if($str.matches('/bdo.com/b'))

true

#else

false

#end

Now reporter is john@do.com, but still this test always returns false. The regex is tested in an online regex tester! What would the proper way to do regex testing on strings in a Velocity template?

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Igor Sereda [ALM Works]
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.
August 24, 2011

1. You need to have \\b instead of /b (two backslashes instead of one for escaping).

2. "matches()" tests the whole string, so you need to match against ".*\\bdo.com\\b.*"

Jan Post August 24, 2011

Thnx, got it working with ".*do.com".

1 vote
Sergey Markovich
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.
August 24, 2011

Hi Jan,

Velocity inherits regex syntax from Java language.

You can have a look at the overview in the documentation here: http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum

I think your code should check something like #if($str.matches("do.com"))

Jan Post August 24, 2011

combining both answers did the trick. thnx

TAGS
AUG Leaders

Atlassian Community Events