• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Regular Expression enhancements in ColdFusion (2018 release) Update 5

Adobe Employee ,
Oct 02, 2019 Oct 02, 2019

Copy link to clipboard

Copied

In Update 5 of the 2018 release of ColdFusion, the application flag useJavaAsRegexEngine was introduced.

If you enable this flag at the application level, you can override the default Regex engine and you can use the Java Regex engine.

For example,

 

 

 

component
{
	this.useJavaAsRegexEngine=true;
}

 

 

 

For more information, see Application.cfc variables.

You can also enable the option at the serve. Enable the option Use Java As Regex Engine, located in Server Settings > Settings of the ColdFusion Administrator.

useJavaAsRegex.png

 

 

 

For more information, see Using the ColdFusion Administrator.

For example, using the default Perl-regex engine, you can write the following snippet:

 

 

 

<cfscript>
     writeOutput(refind("[[:digit:]]","abc 456 ABC 789? Paraguay for $99 airfare!")) // Returns 5
</cfscript>

 

 

 

After you enable the flag useJavaAsRegexEngine, you can rewrite the snippet above as follows:

 

 

 

<cfscript>
     writeOutput(refind("\p{Digit}","abc 456 ABC 789? Paraguay for $99 airfare!")) // Returns 5
</cfscript>

 

 

 

Let us look at a few use cases where you can use the flag and implement Java Regex. Set the flag useJavaAsRegexEngine to true in Application.cfc, as shown below:

 

 

 

component
{
	this.name='SampleApp';
	this.useJavaAsRegexEngine=true;
}

 

 

The examples below use the following Regular Expression functions in ColdFusion:

 

Note: The examples below are also hosted on cffiddle.org. All samples are part of this project,

To copy the project in your workspace in CFFiddle, follow the steps below:

  1. Log in with your Gmail or Facebook credentials.
  2. Navigate to the project in the left pane.
  3. Once you make some changes in any cfm in the project, a pop up displays asking you to save the project.
  4. Give the project a suitable name and click Save.

 

 

Example 1

Search for a Regular Expression that is not present in a base string.

 

 

 

<cfscript>
	basestring = "The quick brown fox jumped over the lazy dog."
	testpattern = "[P-S]araguay"
	// useJavaAsRegexEngine=true
	writeOutput(refind(testpattern,basestring)) // Returns 0
</cfscript>

 

 

 

Example 2

Search for a Regular Expression that is not present in a base string because of case sensitivity.

 

 

 

<cfscript>
	basestring = "The quick brown fox jumped over the lazy dog."
	testpattern = "FO[VWX]"
	// useJavaAsRegexEngine=true
	writeOutput(refind(testpattern,basestring)) // Returns 0
	writeOutput(refindnocase(testpattern,basestring)) // Returns 17
</cfscript>

 

 

 

Example 3

Search for a Regular Expression that is not present in a base string because the offset is after the substring's occurrence.

 

 

 

<cfscript>
    basestring = "The quick brown fox jumped over the lazy dog."
	testpattern = "fo[vwx]"
	offset=20
	// useJavaRegexEngine=true
	writeOutput(refind(testpattern,basestring,offset)) // Returns 0
	writeOutput(refindnocase(testpattern,basestring,offset)) // Returns 0
</cfscript>

 

 

 

Example 4

Search for a Regular Expression that is not present in a base string because the offset is after the basestring's length.

 

 

 

<cfscript>
    basestring = "The quick brown fox jumped over the lazy dog."
	testpattern = "fo[vwx]"
	offset=200
	// useJavaRegexEngine=true
	writeOutput(refind(testpattern,basestring,offset)) // Returns 0
	writeOutput(refindnocase(testpattern,basestring,offset)) // Returns 0
</cfscript>

 

 

 

Example 5

Search for a Regular Expression in a base string, where the base string is an empty string.

 

 

 

<cfscript>
    basestring = ""
	testpattern = "fo[vwx]"
	// useJavaRegexEngine=true
	writeOutput(refind(testpattern,basestring)) // Returns 0
	writeOutput(refindnocase(testpattern,basestring)) // Returns 0
</cfscript>

 

 

 

Example 6

 

 

 

<cfscript>
   basestring="one#chr(10)#two#chr(10)#three#chr(13)#four"
   basepattern="(?m)^two"
   writeoutput(reFind(basepattern,basestring)) // Returns 5
</cfscript>

 

 

 

Example 7

 

 

 

<cfscript>
    basestring="one#chr(10)#two#chr(10)#three#chr(13)#four"
    testpattern="\ntwo"
    writeOutput(refind(testpattern,basestring)) // Returns 4
</cfscript>

 

 

 

Example 8

 

 

 

<cfscript>
    basestring="one#chr(10)#two#chr(10)#three#chr(13)#four"
    testpattern="\rf"
    writeOutput(refind(testpattern,basestring)) //Returns 14
</cfscript>

 

 

 

Example 9

Search for a hard coded character class Regular Expression in a hard coded base string.

 

 

 

<cfscript>
    basestring="The	quick brown fox jumped over the lazy dog."
    testpattern="\s"
    writeOutput(reFindNoCase(testpattern,basestring)) // Returns 4
</cfscript>

 

 

 

Example 10

Search for a hard coded character class Regular Expression in a hard coded base string, starting at a hard-coded offset.

 

 

 

<cfscript>
    basestring="123 abc 456 ABC 789? Paraguay for $99 airfare!"
    testpattern="."
    offset=8
    writeOutput(reFindNoCase(testpattern,basestring,offset)) // Returns 8
</cfscript>

 

 

 

Example 11

Create a Regular Expression and substring as variables. Specification of [A-Z] should be ignored and first alpha character found.

 

 

 

<cfscript>
    basestring = "123 abc 456 ABC 789? Paraguay for $99 airfare!"
    testpattern = "[A-Z]"
    writeOutput(refindnocase(testpattern,basestring)) // Returns 5
    writeOutput(refind(testpattern,basestring)) // Returns 13
</cfscript>

 

 

 

Example 12

Create base string, Regular Expression, and offset as variables. Specification of [a-z] should be ignored and first alpha character found.

 

 

 

<cfscript>
    basestring = "THE QUICK BROWN FOX JUMPED OVER 3 lazy DOGS."
    testpattern = "[a-z]"
    offset = 2
    writeOutput(refindnocase(testpattern,basestring,offset)) // Returns 2
</cfscript>

 

 

 

Example 13

Pass a numeric value instead of a string for the set.

 

 

 

<cfscript>
    basestring = "The quick brown fox jumped over 824 lazy dogs."
    toreplace = 24
    pattern = "zz"
    writeOutput(reReplace(basestring,toreplace,pattern)) // Returns The quick brown fox jumped over 8zz lazy dogs.
</cfscript>

 

 

 

Example 14

Pass a numeric value instead of a string for the base string.

 

 

<cfscript>
    basestring = 111221111445
    set = "1(2|4)"
    newstuff = "++"
    writeOutput(rereplace(basestring,set,newstuff,'all')) // Returns 11++2111++45
</cfscript>

 

 

Example 15

Replace all occurrences of a hard coded character class regular expression in a hard coded base string.

 

 

<cfscript>
    string="123 abc 456 ABC 789? Paraguay for $99 airfare!"
    regex="[\p{Alpha}]"
    substring="~"
    scope="all"
    writeOutput(reReplace(string,regex,substring,scope)) // Returns 123 ~~~ 456 ~~~ 789? ~~~~~~~~ ~~~ $99 ~~~~~~~!
</cfscript>

 

 

Example 16

Create Regular Expression, substring, and basestring as variables.

 

 

<cfscript>
    basestring = "123 abc 456 ABC 789? Paraguay for $99 airfare!"
    testpattern = "[\p{Upper}]"
    newchars = "@'%"
    writeOutput(reReplace(basestring,testpattern,newchars)) // 123 abc 456 @'%BC 789? Paraguay for $99 airfare!
</cfscript>

 

 

Example 17

 

 

<cfscript>
    basestring = "This is a stickup."
    testpattern = "[\p{XDigit}]"
    newchar = "THE"
    scope = 'one'
    writeOutput(rereplace(basestring,testpattern,newchar,scope)) // This is THE stickup.
</cfscript>

 

 

Example 18

 

 

<cfscript>
    basestring = "abcdefghijklmnopqrstuvwxyz1234567890,./?<>;:'[]{}`~!@##$%& ()-_=+ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    testpattern = "[\p{XDigit}]"
    newchar = "*"
    scope = 'all'
    writeOutput(reReplace(basestring,testpattern,newchar,scope)) // Returns
    // ******ghijklmnopqrstuvwxyz**********,./?<>;:'[]{}`~!@#$%& ()-_=+******GHIJKLMNOPQRSTUVWXYZ**********
</cfscript>

 

 

 

TOPICS
Cffiddle , Getting started

Views

4.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2019 Oct 08, 2019

Copy link to clipboard

Copied

This would be a good post on the ColdFusion portal (coldfusion.adobe.com).  I found it interesting but just happened upon it here in the forum. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2022 Nov 04, 2022

Copy link to clipboard

Copied

LATEST

Hey, Grae, I happened to just see your comment here. FWIW, I did just today do a blog post on the CF portal pointing out this feature, as well as pointing out this post of course as well as some others on the topic:

https://coldfusion.adobe.com/2022/11/switching-cf-to-use-java-regex-engine/ 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation