Using Regular Expressions in JSP EL
Asked Answered
D

4

11

In EL expressions, used in a jsp page, strings are taken literally. For example, in the following code snippet

<c:when test="${myvar == 'prefix.*'}">

test does not evaluate to true if the value of myvar is 'prefixxxxx.' Does anyone know if there is a way to have the string interpreted as a regex instead? Does EL have something similar to awk's tilde ~ operator?

Dishpan answered 17/11, 2008 at 17:36 Comment(0)
M
19

While this special case can be handled with the JSTL fn:startsWith function, regular expressions in general seem like very likely tests. It's unfortunate that JSTL doesn't include a function for these.

On the bright side, it's pretty easy to write an EL function that does what you want. You need the function implementation, and a TLD to let your web application know where to find it. Put these together in a JAR and drop it into your WEB-INF/lib directory.

Here's an outline:

com/x/taglib/core/Regexp.java:

import java.util.regex.Pattern;

public class Regexp {

  public static boolean matches(String pattern, CharSequence str) {
    return Pattern.compile(pattern).matcher(str).matches();
  }

}

META-INF/x-c.tld:

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
  <tlib-version>1.0</tlib-version>
  <short-name>x-c</short-name>
  <uri>http://dev.x.com/taglib/core/1.0</uri>
  <function>
    <description>Test whether a string matches a regular expression.</description>
    <display-name>Matches</display-name>
    <name>matches</name>
    <function-class>com.x.taglib.core.Regexp</function-class>
    <function-signature>boolean matches(java.lang.String, java.lang.CharSequence)</function-signature>
  </function>
</taglib>

Sorry, I didn't test this particular function, but I hope it's enough to point you in the right direction.

Marsden answered 17/11, 2008 at 20:3 Comment(0)
M
6

Simply add the following to WEB-INF/tags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib version="2.1"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">

    <display-name>Acme tags</display-name>
    <short-name>custom</short-name>
    <uri>http://www.acme.com.au</uri>
    <function>
        <name>matches</name>
        <function-class>java.util.regex.Pattern</function-class>
        <function-signature>
            boolean matches(java.lang.String, java.lang.CharSequence)
        </function-signature>
    </function>
</taglib>

Then in your jsp

<%@taglib uri="http://www.acme.com.au" prefix="custom"%>
custom:matches('aaa.+', someVar) }

This will work exactly the same as Pattern.match

Mesics answered 23/10, 2013 at 0:50 Comment(1)
Clever solution! But would it be possible to return the match result. I tried something with the java.util.regex.Matcher class but I don't know to make it work with that tld-syntax...Theory
A
5

You can use JSTL functions like so -

<c:when test="${fn:startsWith(myVar, 'prefix')}">

Take a look: http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html

Adp answered 17/11, 2008 at 17:52 Comment(1)
This doesn't answer op's (and my) question: "Does anyone know if there is a way to have the string interpreted as a regex instead?"Allanite
D
1

for using Pattern.matches inside a jsp page in my case it was enough to call java.util.regex.Pattern.matches(regexString,stringToCompare) because you can't import package in jsp

Dunnock answered 5/10, 2017 at 14:54 Comment(2)
Noted should be that this wasn't possible until EL 2.2 was released and the question was asked more than one year before that.Entente
@Entente you are perfectly right, i just shared for everyone are searching now :)Dunnock

© 2022 - 2024 — McMap. All rights reserved.