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

Regex help please...need to grab a part of a string...

Engaged ,
Jul 08, 2009 Jul 08, 2009

Hi there,

I have a dynamic form with dynamic fields and dynamic data types.

What I want to do is check the data-type by grabbing it from the field name.

For example, I have a field name called "formEmail[email]"

The whole string is the field name, but I want to also extract the string between the square brackets. This will help me validate the field when it comes to submission. I will then compare it to a last of valid possibilities.

How can I do this? I know it must require some regex but I have never looked into REGEX....my bad I know...but I promise I will in future!

Mnay thanks,

Mikey.

1.1K
Translate
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
Advisor ,
Jul 08, 2009 Jul 08, 2009

The book "Adobe ColdFusion 8 Web Application Construction Kit, Volume 2: Application Development" by Ben Forta contains a good intro to regular expressions.  The related chapter is available as a PDF on his website ( http://www.forta.com/books/0321515463/ ).

Below is a sample that could be used to extract the string within the brackets.  It works but is verbose.  I suspect that a regular expressions expert could come up with something more compact.  If anyone has a better way to do this please post it.


<!--- we want to get the part inside the brackets --->
<cfset test=ArrayNew(1) />
<cfset test[1]="formEmail[email]" />
<cfset test[2]="formEmail[phone]" />
<cfset test[3]="formEmailBAD" />

<cfoutput>
<cfloop from="1" to="#ArrayLen(test)#" index="idx">
   
    <p>Test value: #HtmlEditFormat(test[idx])#<br />

    <!--- verify that test string is formated correctly --->
    <cfif ReFindNoCase("^formEmail\[\w{1,}]$", test[idx]) neq 0>
       
        <!--- omit "formEmail[" and final "]" to extract the string within the brackets --->
        <cfset target=Mid(test[idx], 11, Len(test[idx]) - 11) />
        Target: #HtmlEditFormat(target)#

    <cfelse>
        Does not match required pattern
    </cfif>


    </p>
</cfloop>
</cfoutput>

Translate
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
Advocate ,
Jul 08, 2009 Jul 08, 2009
LATEST

Mikey,

One question: Is the string 'formEmail[email]' the actual string (as opposed to being a variable that will get evaluated)?

There are a lot of ways to go about this sort of task with regular expressions. If 'formEmail[email]' is an example of the type of strings you are searching, a regular expression along the lines of the following might be useful:

<cfscript>

     mystring = "formEmail[email]";

     results = ReMatch( "[(\w*)]+", mystring );

</cfscript>

<cfdump var="#results#">

<cfoutput>

     First match: #results[1]#<br />

     Second match: #results[2]#<br />

</cfoutput>

This regular expression finds all words in a string but does not match the brackets. \w is a special character that matches any word character -- including alphanumeric chars and underscores.

The brackets used in the regular expression are also special characters for groupingt, which you can learn more about in the section of the CF WACK book mentioned JR's reply.

If you run this snippet, you'll notice you get an array back. The first element of the array is the first match of the regexp, or the string 'formEmail', and the second element of the array is the second match of the regexp, or the string 'email'.

This regular expression might be helpful if you don't have consistent text before the brackets but you know the string you'll search is formatted as: string[string].

For example, the ReMatch expression above works the same for either of the strings below:

formPhoneNumber[phone] (would return two strings: formPhoneNumber and phone)

formEmail[email] (would return two strings: formEmail and email)

As you delve into the world of Regular Expressions, be sure to grab RegExr, an Adobe AIR application for testing regular expressions. Very handy, especially coupled with a reading of the RegExp section in Forta's CF WACK book!

http://gskinner.com/RegExr/desktop/

Translate
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