Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use java String.matches(str) method in Velocity Templates

I'm attempting to do a match on the user agent in velocity templates.

$ua does print out but Matches! does not. What am I doing wrong?

#set( $ua = $request.getHeader('User-Agent'))
<p>$ua</p>
#if( $ua.matches('/Windows.(NT|XP|ME|9)/')) 
<p>Matches!</p>
#end
like image 637
old_adam Avatar asked Mar 29 '26 16:03

old_adam


1 Answers

I know this is old, but the solution to this problem is that the regex provided to matches must match the ENTIRE string in order to return true. So, for example:

$ua.matches('Windows.(NT|XP|ME|9)')

returns false, but

$ua.matches('.*Windows.(NT|XP|ME|9).*')

will behave as you expect, returning true if Windows.(...) is in the string.

This is a bit odd and really stymied me for a while today.

PS - no slashes are required in the regex!

like image 59
EricTheBiking Avatar answered Apr 02 '26 04:04

EricTheBiking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!