2010-04-14 - Jakarta Taglibs has been retired.

For more information, please explore the Attic.

Jakarta Project: Regexp Tag library

Version: 1.0.1

Table of Contents

Overview

The Regexp custom tag library contains tags which can be used to perform Perl syntax regular expressions.

These tags implement the three most common Perl5 operations involving regular expressions:

As with Perl, any non-alphanumeric character can be used in lieu of the slashes.

For more information on how to use Perl syntax regular expressions you can start at the Perl Regular Expression page. http://www.perl.com/pub/doc/manual/html/pod/perlre.html

If you search the web you can find thousands of web sites documenting how to use perl regular expressions.

Requirements

This custom tag library requires no software other than a servlet container that supports the JavaServer Pages Specification, version 1.1 or higher. And Version 2.0 of the Jakarta ORO Perl Regular Expression package.

Configuration

Follow these steps to configure your web application with this tag library:

To use the tags from this library in your JSP pages, add the following directive at the top of each page:

<%@ taglib uri="http://jakarta.apache.org/taglibs/regexp-1.0" prefix="rx" %>

where "rx" is the tag name prefix you wish to use for tags from this library. You can change this value to any prefix you like.

Tag Summary

Regexp Tags
regexpCreate a regular expression script variable.
text Create a text string script variable for use with a regexp.
existsMatch Uses a regexp to determine if a match exists in the text.
substitutePerforms a string substitution on text using a regexp.
splitImplements a perl style split on the text.
match Loops through each match found in a text string by a regexp.
group Get the value of a single parenthesized group within a single match.
 

Tag Reference

regexp Availability: 1.0

Create a regular expression script variable.

Tag BodyJSP    
Restrictions

The body of the tag is assigned to a script variable as a regular expression.

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

Script variable id for use with standard jsp:getProperty tag and as an attribute to other tags in this tag library.

VariablesNameScopeAvailability
  id attribute value   Start of tag to end of page  1.0
 

Regular Expression Bean

 PropertiesNameGetSetAvailability
   regexp Yes No 1.0
  

Returns the text of the regular expression.

Examples Create a match regular expression scripting variable.  
 


<rx:regexp id="rx1">m/test1/mi</rx:regexp>
The regular expression is:
<jsp:getProperty name="rx1" property="regexp"/>
          

text Availability: 1.0

Create a text string script variable for use with a regexp.

Tag BodyJSP    
Restrictions

The body of the tag is assigned to a script variable as a text string.

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

Script variable id for use with standard jsp:getProperty tag and as an attribute to other tags in this tag library.

VariablesNameScopeAvailability
  id attribute value   Start of tag to end of page  1.0
 

Text String Bean

 PropertiesNameGetSetAvailability
   text Yes No 1.0
  

Returns the text string.

ExamplesCreate a text string scripting variable. 
 


<rx:text id="tx1">This is a test string.</rx:text>
The text string is:
<jsp:getProperty name="tx1" property="text"/>
          

existsMatch Availability: 1.0

Uses a regexp to determine if a match exists in the text.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 regexp  Yes   No  1.0
 

Script variable id of regexp to use for match.

 text  Yes   No  1.0
 

Script variable id of text to check for a match.

 value  No   No  1.0
 

If value is set to false the existsMatch tag includes body of tag if a match does not exist. Default is true.

VariablesNone
Examples Determine if a match can be found in a text string using a regular expression.  
 


<%-- create the match regexp "m/test1/mi" --%>
<rx:regexp id="rx1">m/test1/mi</rx:regexp>
<%-- set the text to match on --%>
<rx:text id="test">
This is a
test of regular expression matching.
</rx:text>
See if a match exists...<br>
<rx:existsMatch regexp="rx1" text="test">
     A match was found!<br>
</rx:existsMatch>
<rx:existsMatch regexp="rx1" text="test" value="false">
     A match was not found!<br>
</rx:existsMatch>
          

substitute Availability: 1.0

Performs a string substitution on text using a regexp.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 regexp  Yes   No  1.0
 

Script variable id of regexp to use for substitute.

 text  Yes   No  1.0
 

Script variable id of text to perform substitution on.

VariablesNone
Examples Substitute "test2" for "test1" in a text string.  
 


<%-- create the substitute regexp "s/test1/test2/gmi" --%>
<rx:regexp id="rx2">s/test1/test2/gmi</rx:regexp>   
<%-- set the text to match on --%>                        
<rx:text id="test">                                    
This is                                 
test1 of regular expression substitution.
</rx:text>                         
Here is the text after substitution...<br>
<rx:substitute regexp="rx2" text="test"/>
          

split Availability: 1.0

Implements a perl style split on the text.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

Script variable id for use with standard jsp:getProperty tag.

 regexp  No   No  1.0
 

Script variable id of regexp to use for split. If no regexp is used the split will be done based on whitespace.

 text  Yes   No  1.0
 

Script variable id of text to perform split on.

 limit  No   No  1.0
 

Set a limit for the number of strings split out from text.

VariablesNameScopeAvailability
  id attribute value   Nested within tag  1.0
 

Split Data Bean

 PropertiesNameGetSetAvailability
   split Yes No 1.0
  

Returns the string split out from text.

Examples First perform a split of a text string on whitespace, then perform a split on the character ','.  
 


<%-- set the text to split --%>
<rx:text id="test">
This is
test1 of a regular expression split,
on whitespace, and then on a comma.
</rx:text>
<ul>
<rx:split id="split" text="test">
   <jsp:getProperty name="split" property="split"/><br>
</rx:split>
</ul>
<%-- now split the text using a comma with limit of 2 --%>
<rx:regexp id="rx3">/,/m</rx:regexp>
<ul>
<rx:split id="split" text="test" regexp="rx3" limit="2">
   <jsp:getProperty name="split" property="split"/><br>
</rx:split>
</ul>
          

match Availability: 1.0

Loops through each match found in a text string by a regexp.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

Script variable id for use with standard jsp:getProperty tag.

 regexp  Yes   No  1.0
 

Script variable id of regexp to use for match.

 text  Yes   No  1.0
 

Script variable id of text to perform match on.

VariablesNameScopeAvailability
  id attribute value   Nested within tag  1.0
 

Match Data Bean

 PropertiesNameGetSetAvailability
   match Yes No 1.0
  

Returns the entire string that was matched on.

   preMatch Yes No 1.0
  

Returns the string preceding the current match.

   postMatch Yes No 1.0
  

Returns the string after the current match.

Examples Use match to find all words in a text string that start with the letter 'i'.  
 


<%-- set the text to perform match --%>
<rx:text id="test">            
This is                              
test1 of a regular expression match,
on words that start with 'i' like item and
index.                               
</rx:text>                     
<%-- create the match regexp "m/\si[^\s]*\s/mi" --%>
<rx:regexp id="rx4">m/\si[^\s]*\s/mi</rx:regexp>
<pre>                          
<rx:match id="mtch" text="test" regexp="rx4">
   Match: <jsp:getProperty name="mtch" property="match"/><br>
    Pre Match: <jsp:getProperty name="mtch" property="preMatch"/><br>
    Post Match: <jsp:getProperty name="mtch" property="postMatch"/><br>
</rx:match>
          

group Availability: 1.0

Get the value of a single parenthesized group within a single match.

Tag Bodyempty    
Restrictions

Must be nested inside a match tag.

AttributesNameRequired Runtime Expression Evaluation Availability
 number  Yes   No  1.0
 

Parenthesised group number to get from match.

VariablesNone
Examples Use the group tag to get the next word in a text string after a word that starts with the letter 'i'.  
 


<%-- set the text to perform match --%>
<rx:text id="test">            
This is                              
test1 of a regular expression match,
on words that start with 'i' like item and
index, the group tag will be used to get the
next word after the word starting with 'i'.
</rx:text>                     
<%-- create the match regexp "m/\si[^\s]*\s([^\s]+)/mi" --%>
<rx:regexp id="rx4">m/\si[^\s]*\s([^\s]+)/mi</rx:regexp>
<pre>                          
<rx:match id="mtch" text="test" regexp="rx4">
   Match: <jsp:getProperty name="mtch" property="match"/><br>
    Parenthesized Group 1: <rx:group number="1"/>
</rx:match></pre>
          

Examples

See the example application regexp-examples.war for examples of the usage of the tags from this custom tag library.

Java Docs

Java programmers can view the java class documentation for this tag library as javadocs.

Revision History

Review the complete revision history of this tag library.