House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  Regular Expressions (RegEx)

How to find the last comma and the word following it w/regex

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
MotoManiac
03/07/2012 02:48 PM

I have a dynamically created a comma delimited string via form submission: i.e. "Monday, Tuesday, Thursday, Saturday" OR "Tuesday, Thursday" etc. Pretty standard stuff. I have already cleaned the string to remove any preceeding or trailing comma's and trimmed all the whitespace. My question is, using regex and coldfusion, how would I find the last occurance of a comma in the string (between Thursday and Saturday for example), and replace that comma withh the word "and" so my string would wind up being "Monday, Tuesday, Thursday and Saturday". Any help would be appreciated. thank you. -- View this message in context: http://old.nabble.com/How-to-find-the-last-comma-and-the-word-following-it-w-regex-tp33460091p33460091.html Sent from the Cold Fusion - RegEx mailing list archive at Nabble.com.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
03/07/2012 03:02 PM

You can use $ to match the end of the string, and [^,] to match "anything except a comma". So to find the last comma, you want to match a comma, followed by "anything except a comma" as many times as necessary until the end of the string. To then replace the comma, but not the non-comma text afterwards, we can use a lookahead - using syntax (?=X) to say "make sure X matches the following text, but don't include it in the match/replacement. Putting all that together, the regex would be: ,(?=[^,]+$) And that can be used with: <cfset Text = REReplace( Text , ',(?=[^,]+$)' , ' and' ) /> Hopefully that all makes sense?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
MotoManiac
03/07/2012 03:17 PM

Peter Boughton wrote: > > Hopefully that all makes sense? > Yes that makes perfect sense, it works perfectly! You are a lifesaver Peter, thank you! David Pierce -- View this message in context: http://old.nabble.com/How-to-find-the-last-comma-and-the-word-following-it-w-regex-tp33460091p33460736.html Sent from the Cold Fusion - RegEx mailing list archive at Nabble.com.


<< Previous Thread Today's Threads Next Thread >>

Search regex

May 26, 2013

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31   

Designer, Developer and mobile workflow conference