[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help retrieving substrings using REGEX (Coldfusion 8)

Posted on 2009-04-24
11
Medium Priority
?
558 Views
Last Modified: 2012-05-06
I need a function which returns certain substrings of a slightly larger string. Sample string would be:

"[one][two][three]", would return "one", "two", "three".

I cannot just split using "[]" as delimiters how ever since anything could appear imbetween the entries, i.e:

"abc [one]/[two] good morning [three].pdf", and this should still return "one", "two", "three".

Left to my own devises i would be inclined to get the indexes of "[" and "]" alternatively, grabbing subsequent substrings, but think this is both inelegant and likely slow?? Would i be right in thinking that best solution would involve regex? I am not familiar with the regex expression paths, so if this is the correct tool to use can anyone provide me a simple code sample (in Coldfusion) for retrieving the values for the sample output above? The response can be an array, list or anything manageable.

If regex is not the best way then perhaps you can suggest something better. Side note: the sample input is typically no more than 40-50 characters.

Thanks :)
0
Comment
Question by:paddycobbett
  • 6
  • 5
11 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24229608
Does the following give you what you need? (Forgive me if the syntax is bad--I'm not a CF programmer) ;)
# <cfset arrTitles = REMatch(
# "\[(\w+\)]",
# string_to_search
# ) />

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24229613
Line 2 of the previous post is the important part. I *believe* REMatch is what you need to use.
0
 
LVL 1

Author Comment

by:paddycobbett
ID: 24229765
Thanks alot for the response, yes you're quite right that REMatch is the correct coldfusion function :)

However i get an error "Unmatched parenthesis" which i suspect is thrown by the regex engine. Are you sure the expression path is syntatically correct? If you can double check that then i can be sure it's some other cause.
<cfset path="[one]  [two] [three]">
<cfset arrTitles = REMatch("\[(\w+\)]", path) />
 
<cfdump var="#arrTitles#">

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 24229834
Oh shoot, typo!! Try:
REMatch("\[(\w+)\]"

Open in new window

0
 
LVL 1

Author Closing Comment

by:paddycobbett
ID: 31574415
Spot on! Works perfect with all my samples, thanks! :)
0
 
LVL 1

Author Comment

by:paddycobbett
ID: 24229906
Oh shoot, jumped the gun also!! Could you please ammend so that the result values don't include the "[" and "]"? So the values returned are "One" and not "[One]" as specified in original question. Sorry and thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24230022
That's the part I'm not sure about. Under normal RegEx, the parentheses I included would have captured  just the part between them. I'll do some more research, but I am fearful that you may have to take that array that is returned and then remove the parens when you process them.

I'll post back if I find anything helpful.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24230098
It seems as though the CF regex engine isn't all that powerful on its own. To get the power you are searching for, it is suggested to use the Java regex engine. This link talks about it (toward the middle of page).

http://www.bennadel.com/blog/769-Learning-ColdFusion-8-REMatch-For-Regular-Expression-Matching.htm

The patter should be as follows:
REMatch("(?<+\[)\w+(?=\])"

Open in new window

0
 
LVL 1

Author Comment

by:paddycobbett
ID: 24230140
Ok thanks again for coming back to the question, i will remove the brackets from the result which will work for now.
0
 
LVL 1

Author Comment

by:paddycobbett
ID: 24230948
Hello again! :)

The solution breaks when there are spaces in the square brackets, for example:

"[good morning] [hello]" only returns "[hello]"

I'm guessing this is a straight forward fix (for those that know how!), however if not let me know and will investigate this myself.
0
 
LVL 1

Author Comment

by:paddycobbett
ID: 24230956
I think it only fair that i present the space requirement in a new question:

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_24354552.html
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question