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

  • SEARCH
  • Home
  • 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 8156521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:58:45+00:00 2026-06-06T16:58:45+00:00

This is a follow-up to my previous question I would like to find a

  • 0

This is a follow-up to my previous question

I would like to find a minimal sequence of characters of length > N, which starts at a word boundary and ends at the end of input.

For example:

N = 5, input = "aaa bbb cccc dd"
result = "cccc dd"

I tried \b.{5,}?$ but it matches the whole input rather the minimal part.

What regex would you suggest?

  • 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-06-06T16:58:47+00:00Added an answer on June 6, 2026 at 4:58 pm

    The problem this time isn’t greediness, it’s eagerness. Regexes naturally try to find the earliest possible match, and getting them to find the last one can be tricky. The easiest way is usually the one @Arcadien demonstrated: use .* to gobble up the whole string, then use backtracking to find the match on the rebound.

    I have some questions about your requirements, though. \b can match the beginning or the end of a word, so if (for example) N=5 and the string ends with "foo1 bar2", the result would be " bar2" (notice the leading space). Do you really want a match that starts at the end of a word, or should it drop the space or back up to the beginning of "foo1"? Also, will all words consist entirely of word characters? If there are any non-word characters, \b will be able to match in even more surprising places.

    For the regex below, I redefined “word” to mean any complete chunk of non-whitespace characters. The .* starts out by consuming the whole string, then the lookahead – (?=.{5,}) – forces it to backtrack five positions before it tries to match anything. The \s forces the match to start at the beginning of a word, so the rest of the regex captures one or more complete words.

    /^.*(?=.{5,})\s(\S+(?:\s+\S+)*$)/
    
    var N = 5;
    var regex = "^.*(?=.{" + N + ",})\\s(\\S+(?:\\s+\\S+)*$)";
    var match = regex.exec(subject);
    var result = (match != null) ? match[1] : "";
    

    This regex won’t match anything that’s less than five characters long or doesn’t contain whitespace. If that’s a problem, let me know and I’ll tweak it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up to my previous question . I still find it very
OK, this question is actually a follow-up question from my previous one: What would
This is a follow-up to my previous question. Given function add_stream(s1:Stream[Int], s2:Stream[Int]):Stream[Int] I would
This is a follow up question to a previous question I asked about calculating
This is a follow up from my previous question I have this code basically
This is a follow-up to my previous question . Suppose I am writing the
This is a follow up to my previous question: Problem passing parameters via Iframe
This is a follow on to my previous question: JSP / servlet / IE
This is a follow on from my previous question although this is about something
This is a follow up to my previous question . My code is still

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.