FileDocCategorySizeDatePackage
RegexUtil.javaAPI DocGlassfish v2 API4240Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

RegexUtil

public final class RegexUtil extends Object
Useful utilities for regex handling

Fields Summary
private static final char
BACKSLASH
public static final String
REGEX_SPECIALS
These characters will be escaped by wildcardToJavaRegex()
Constructors Summary
private RegexUtil()

		// disallow instantiation
	
Methods Summary
public static java.util.regex.Pattern[]exprsToPatterns(java.lang.String[] exprs)
Converts each String to a Pattern using wildcardToJavaRegex

param
exprs String[] of expressions
return
Pattern[], one for each String

	
	
	
			       				 	  			    	 
		  
	  	 
	
		return( exprsToPatterns( exprs, 0 ) );
	
public static java.util.regex.Pattern[]exprsToPatterns(java.lang.String[] exprs, int flags)
Converts each String to a Pattern using wildcardToJavaRegex, passing the flags.

param
exprs String[] of expressions
param
flags flags to pass to Pattern.compile
return
Pattern[], one for each String

		final Pattern[]	patterns	= new Pattern[ exprs.length ];
		
		for( int i = 0; i < exprs.length; ++i )
		{
			patterns[ i ]	= Pattern.compile( wildcardToJavaRegex( exprs[ i ]), flags  );
		}
		return( patterns );
	
public static java.lang.StringwildcardToJavaRegex(java.lang.String input)
Supports the single wildcard "*". There is no support for searching for a literal "*". Convert a string to a form suitable for passing to java.util.regex.

		String	converted	= input;
		
		if ( input != null )
		{
			final int 			length	= input.length();
			final StringBuffer	buf	= new StringBuffer();
			
			for( int i = 0; i < length; ++i )
			{
				final char	theChar	= input.charAt( i );
				
				if ( theChar == '." )
				{
					buf.append( "[.]" );
				}
				else if ( theChar == '*" )
				{
					buf.append( ".*" );
				}
				else if ( REGEX_SPECIALS.indexOf( theChar ) >= 0 )
				{
					// '[' begins a set of characters
					buf.append( "" + BACKSLASH + theChar );
				}
				else
				{
					buf.append( theChar );
				}
			}
			
			converted	= buf.toString();
			
		}
		return( converted );