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 8751175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:59:45+00:00 2026-06-13T12:59:45+00:00

I found some partial help but cannot seem to fully accomplish what I need.

  • 0

I found some partial help but cannot seem to fully accomplish what I need. I need to be able to do the following:

I need an regular expression to replace any 1 to 3 character words between two words that are longer than 3 characters with a match any expression:

For example:

walk to the beach ==> walk(.*)beach

If the 1 to 3 character word is not preceded by a word that’s longer than 3 characters then I want to translate that 1 to 3 letter word to '<word> ?'

For example:

on the beach ==> on ?the ?beach

The simpler the rule the better (of course, if there’s an alternative more complicated version that’s more performant then I’ll take that as well as I eventually anticipate heavy usage eventually).

This will be used in a PHP context most likely with preg_replace. Thus, if you can put it in that context then even better!

By the way so far I have got the following:

$string = preg_replace('/\s+/', '(.*)', $string); 
$string = preg_replace('/\b(\w{1,3})(\.*)\b/', '${1} ?', $string);

but that results in:

walk to the beach ==> 'walk(.*)to ?beach' 

which is not what I want. 'on the beach' seems to translate correctly.

  • 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-13T12:59:46+00:00Added an answer on June 13, 2026 at 12:59 pm

    I think you will need two replacements for that. Let’s start with the first requirement:

    $str = preg_replace('/(\w{4,})(?: \w{1,3})* (?=\w{4,})/', '$1(.*)', $str);
    

    Of course, you need to replace those \w (which match letters, digits and underscores) with a character class of what you actually want to treat as a word character.

    The second one is a bit tougher, because matches cannot overlap and lookbehinds cannot be of variable length. So we have to run this multiple times in a loop:

    do
    {
        $str = preg_replace('/^\w{0,3}(?: \w{0,3})* (?!\?)/', '$0?', $str, -1, $count);
    } while($count);
    

    Here we match everything from the beginning of the string, as long as it’s only up-to-3-letter words separated by spaces, plus one trailing space (only if it is not already followed by a ?). Then we put all of that back in place, and append a ?.

    Update:

    After all the talk in the comments, here is an updated solution.

    After running the first line, we can assume that the only less-than-3-letter words left will be at the beginning or at the end of the string. All others will have been collapsed to (.*). Since you want to append all spaces between those with ?, you do not even need a loop (in fact these are the only spaces left):

    $str = preg_replace('/ /', ' ?', $str);
    

    (Do this right after my first line of code.)

    This would give the following two results (in combination with the first line):

    let us walk on the beach now go => let ?us ?walk(.*)beach ?now ?go
    let us walk on the beach there now go => let ?us ?walk(.*)beach(.*)there ?now ?go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found some classes designed for debugging in package com.sun.jdi like VirtualMachine , but
I need a regular for time format HH:MM am/pm. I found something like $time
I'm new in .NET development and need some help. I need to display stars
I've done some searching on this, and I've found several partial answers, however nothing
I was looking about some GDI tutorial but everything I have found so far
I found some lovely websites - http://www.mini.jp/event_campaign/big-point/ , http://www.twenty8twelve.com/ and http://www.scozzese.com - all vertical
I found some problems when implementing Jake Wharton 's ViewPagerIndicator . These are what
I found some things I want to submit a pull request for in the
I found some old Python code that was doing something like: if type(var) is
I found some really great code from Matt Gallagher for use with making Undo

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.