Skip to main content
Participant
February 21, 2012
Question

Regex non greedy match.

  • February 21, 2012
  • 1 reply
  • 1181 views

In the below code I am attempting to match and then replace all the text apart from the uppercase stuff... It is uppercase to clearly point out the text I wish to keep but in the real world they could be any characters.

Ufortunately coldfusion is doing a greedy search on this and stripping out everything between the first and last parentheses.

I would like it to first match the 'removethis(delete me)' and then move onto the second "removethis(delete me)" text , ignoring all the other stuff.

Any help would be great.

Thanks!

strTest = 'removethis(delete me) DONT DELETE THIS removethis(delete me) DONT DELETE THIS';

strReFind = "\s*\w*\s*\(\s*\)";

strParse = reReplaceNoCase(strTest , strReFind , '');

dbort(strParse);

    This topic has been closed for replies.

    1 reply

    Inspiring
    February 21, 2012

    Oddly, it's not in the CF docs - http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html -  (that I can see), but the ? qualifier works in this case to make the * non-greedy.

    The docs for the underlying engine (which is long since dead) are here:

    http://jakarta.apache.org/oro/api/index.html

    --

    Adam

    cheggersAuthor
    Participant
    February 21, 2012

    Thankyou very much for your speedy response.. Will take a look at the engine....

    In retrospect I should have delayed my post since I figured out a work around.

    I decided to match everything within parentheses except for a parentheses itself... which has gotten my desired result.

    You may have realised that I am relatively new to regex.

    Thanks again, Hope you have a charming day!




    strTest = 'leave me stinky(DELETEMEE)dont get rid of me plonky(DELETE MEE) leave me';



    strReFind = "\s*\w*\s*\([^\(\)]*\)";



    strParse = reReplaceNoCase(strTest , strReFind , '');











    dbort(strParse);