Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 495587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:36:27+00:00 2026-05-13T05:36:27+00:00

I am stuck up with a problem while using Regular Expression. My requirement is

  • 0

I am stuck up with a problem while using Regular Expression.
My requirement is : split a long string into maximum size of 125 letters and then insert a line break in between them.
while splitting, it shouldn’t split between the words. in short, i want to split a string into small strings whose length is 125 or at the end of word before 125th letter. Hope i didnt confused

i used one regexp to solve this, and believe me am an absolute zero in this.
i just got one code and copy pasted 😉

StringBuffer result = null;  
while(mailBody.trim().length() > 0){  
    Matcher m = Pattern.compile("^.{0,125}\\b").matcher(mailBody);  
    m.find();  
    String oneLineString = m.group(0);  
    if(result == null)  
        result = new StringBuffer(oneLineString);  
    else  
        result.append("\n"+ oneLineString);  
    mailBody = mailBody.substring(oneLineString.length(),
                                  mailBody.length()).trim();  
}    

this is my code, and it’s working perfectly unless the starting string ends with a full stop(.).
In that case it is giving an error like : No match found.

Please help.

Regards,
Anoop P K

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:36:27+00:00Added an answer on May 13, 2026 at 5:36 am

    Can you try using the following instead?

    Matcher m = Pattern.compile("(?:^.{0,125}\\b)|(?:^.{0,125}$)").matcher(mailBody);  
    

    Here we use your original match OR we match a string whose total length is 125 characters or fewer. The (?:X) items are non-capturing groups, so that I can use the | operator on the large groups.

    (See documentation for the Pattern class here.)


    Addendum: @Anoop: Quite right, having sentence-ending punctuation left off on its own line is undesirable behavior. You can try this instead:

    if(result == null)  
       result = new StringBuffer("");
    
    mailBody = mailBody.trim();
    
    while(mailBody.length() > 125) {
    
        // Try not to break immediately before closing punctuation
        Matcher m = Pattern.compile("^.{1,125}\\b(?![-\\.?;&)])").matcher(mailBody);
        String oneLineString;
    
        // Found a safe place to break string
        if (m.find()) {
    
            oneLineString = m.group(0);
    
        // Forced to break string in an ugly fashion
        } else {
    
            // Try to break at any word boundary at least
            m = Pattern.compile("^.{1,125}\\b").matcher(mailBody);
    
            if (m.find()) {
    
                oneLineString = m.group(0);
    
            // Last ditch scenario, just break at 125 characters
            } else {
    
                oneLineString = mailBody.substring(0,124);
    
            }
    
        }
    
        result.append(oneLineString + "\n");
        mailBody = mailBody.substring(oneLineString.length(),
                                      mailBody.length()).trim();  
    }
    
    result.append(mailBody);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer https://msdn.microsoft.com/en-us/library/ms750441(v=vs.100).aspx has detailed information about the architectural design of WPF… May 17, 2026 at 3:06 am
  • Editorial Team
    Editorial Team added an answer Try this: <div style="border:solid 1px #D31444" contenteditable="true" onclick="document.execCommand('selectAll',false,null)">12 some text...</div>… May 17, 2026 at 3:06 am
  • Editorial Team
    Editorial Team added an answer Assert.That(schedule.PendingItems, Has.No.Member(item)) Only with NUnit 2.4 / 2.5 May 17, 2026 at 3:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm stuck on a RegEx problem that's seemingly very simple and yet I can't
we have a weired problem when using Rhino Mocks and Threads. I've tried to
Let's consider the two following lines in C# (using framework .NET 3.5) Regex regex
I am using Jquery Validation . Currently, I have a username, what i want
I have a scheduled task that runs a script on a regular basis (every
I've got a global Graphics object created from a Panel. At regular intervals an
I'm stuck at not being able to map texture to a square in openGLES.
I've only dealt with one-to-one relationships in php so far, but I'm stuck on
I am using the FPDF library for generating PDF files by PHP. This library
I'm stuck behind a firewall, so I have to use HTTPS to access my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.