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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:03:53+00:00 2026-05-13T06:03:53+00:00

is it possible to get a regular expression that do: find first occurence of

  • 0

is it possible to get a regular expression that do:

  1. find first occurence of some word in a string
  2. return a substring of a nr of letters before and after the occurence
  3. but if it encounters a . (dot) before the nr of letters before and after occurence, it will just return the sub string before/after the dot.
  4. return whole words

example:

“Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla”

if the keyword is ‘program’ and we put nr of letters to 20 it will return 20 letters before and after ‘program’. But since it encounters a dot before it gets to 20 letters it will stop there.

“Create a program that inputs a regular…”

Is this possible with regexp? what php function do i have to use? is there any finnished script for this? I guess its a quite basic need when showing search results. Someone already got the function to share?

  • 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-13T06:03:53+00:00Added an answer on May 13, 2026 at 6:03 am

    Here’s Dav’s regular expression in php:

    <?php
      $str = "Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla";
      $key = "program";
      $lim = 20;
      $reg = "/([^.]{0,{$lim}})({$key})([^.]{0,{$lim}})/"; // /[^.]{0,20}program[^.]{0,20}/
    
      $res = preg_match($reg, $str, $matches);
      echo $matches[0];
    
      print_r($matches); // $matches[1] is the pre-text, and $matches[3] is the post-text
    

    The trickiest requirement is #4: “return whole words”. One way you could handle this while still making use of the above regular expression is pull more text before and after than you really want (say 40 chars). Then you could preg_split the before and after text on whitespace, which would give you two arrays of words. Run the arrays through a function that gives you back a subset of the array where the total length of all the words is less than your limit of 20…

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

Sidebar

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.