Skip to content

Simple Pattern Matcher

thofrey edited this page Apr 1, 2014 · 7 revisions

Available in Mach-II 1.8+

Table of Contents

  1. Overview
  2. Public Methods

Overview

The MachII.util.SimplePatternMatcher (1.8) or MachII.util.matching.SimplePatternMatcher (1.9+) is a new utility introduced in Mach-II 1.8 that makes matches by allowing you to use a simple * wildcard. This allows you to check a string against a pattern using the * wildcard.

Example Patterns

**Pattern ** Text to Match ** Does Pattern Match?**
[nothing] [nothing] false (no pattern)
1 [nothing] false
* Mach-II true
123 123 true (exact match)
get* getMe true
get* setMe false
*stuff* getMeTest false
*stuff* getstuffTest true
3*3 3 false
3*3 33 true
12*45*78 12345678 true
12*45*78 123456789 false

For complex pattern matching when a path separator is being used such as a file path (/ or \) or url (/) use the ANT Style Pattern Matcher.

Public Methods

The init method

This method initializes the simple pattern matcher and takes no arguments..

This is how you would create an instance of the utility (1.8):

    <!--- Using the default path separator --->
    <cfset matcher = CreateObject("component", "MachII.util.SimplePatternMatcher").init() />

This is how you would create an instance of the utility (1.9+):

    <!--- Using the default path separator --->
    <cfset matcher = CreateObject("component", "MachII.util.matching.SimplePatternMatcher").init() />

The isPattern method

This method checks if the passed path has a pattern in it by checking to see if the pattern has any *'s in it and returns a boolean result.

Example code:

    <cfoutput>
        Is 'test.*' a pattern:(/machii/wiki/RemovingEventHandlersForSecurity) #matcher.isPattern("test.*")#</br>
        Is 'test...' a pattern: #matcher.isPattern("test...")#
    </cfoutput>

Example output:

    Is 'test.*' a pattern: true
    Is 'test...' a pattern: false

The match method

Performs a match of simple a pattern or array of patterns against the text. It can perform a match against a single pattern or an array of patterns.

Example code using a single pattern:

    <cfoutput>Does this pattern match: #matcher.match("give*", "giveMeLotsOfMoney")#</cfoutput>

Example output using a single pattern:

    Does this pattern match: true

Example code using multiple patterns in an array:

    <cfset patterns = ArrayNew(1) />
    <cfset patterns[1] = "123*" />
    <cfset patterns[2] = "abc*" />
    <cfset patterns[3] = "m2*" />
    <cfoutput>Does this pattern match: #matcher.match(patterns, "12345678")#</cfoutput>

Example output using multiple patterns in an array:

Does this pattern match: true (matches the 123* pattern)

For a real world example of the simple pattern matcher in use, check out Removing Event-Handlers for Security Reasons wiki entry.

Clone this wiki locally