Using coldfusion regex to cut a too long word

Posted on 2009-02-12
Last Modified: 2013-12-16
Hi, I want to cut words of more than n characters in 2 parts like n=10: nnnnnnnnnn = nnnnn- nnnnn. or nnnn- nnnnnn.
I would like to use regex for that, thanks!
Question by:khristofe
    LVL 16

    Accepted Solution

    Right, this is completely untested:

    <cfset string = "abcdefghij">

    <cfset newstring = REReplaceNoCase(string, "([[:print:]]{5})([[:print:]]{5})", "\1-\2")>

    Basically I'm using parentheses ( ) around two parts.  The {5} means give me exactly 5 characters.  So we're looking for a string that has 5 characters, then another 5 characters.  This regex isn't very advanced, so if your string is longer than 10 characters, the extra bits aren't taken into consideration.  

    The replace part then says give me the first bit in parentheses (the first 5 characters), then a hyphen, then the second set of 5 characters.  

    Author Comment

    That is working, but i would like to modify all words in this text that have more than 10 characters separating charcters with an hyphen after char 5 for example...
    And then output the final text with all those words with hyphen after the character 5...

    Loremipsum dolor sit amet, consectetur adipiscing elit. Crainsapien. Sed dapibussagittis quam. Vivamus est erat, elementumid, pulvinar id, aliquam et, metus. Proin vestibulum eros ac dui consequat porta.


    Author Comment

    The idea was to use regex to hyphenize long words into a div with justify text on it, CSS3 allows to do that using WORD-WRAP, but CSS2 don't... is it possible to use a script for that?

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
    This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
    The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: : Click on Too…
    The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now