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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:10:28+00:00 2026-05-16T11:10:28+00:00

I wrote a regex, but it doesn’t work as i expect. Take a look

  • 0

I wrote a regex, but it doesn’t work as i expect. Take a look please


preg_match_all("/([\.\:]?)(.{0,65}?[^\s]*".preg_quote($word)."[^\s]*.{0,65})/siu",$content,$matched);

[^\s]*".preg_quote($word)."[^\s]//

this part match the whole word, if it contain the keyword, for example it match keyword if i search for wor keyword.

.{0,65}?[^\s]*".preg_quote($word)."[^\s]*.{0,65}

here i get up to 65 characters before and after the keyword, ie. i will get

many words here keyword and other words here


And now, what is the problem. I try to match the sentence from the begining, if there is any of [.:] characters within {65} characters

if i have sach structure – word1 word2 . {less then 65 character here} keyword {other characters here}

i expect, then if i wrote ([\.\:]?)(.{0,65}?[^\s]*".preg_quote($word)."[^\s]*.{0,65})

it will match .{less then 65 character here} keyword {65 characters}

but it doesn’t. the part [\.\:]? hasn’t any influence on regex. it match all {65} characters.

i need to match sentence from begining, if the start of sentence within 65 characters before the keyword

  • 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-16T11:10:29+00:00Added an answer on May 16, 2026 at 11:10 am

    [.:]? means “match a dot (.), a colon (:), or nothing”; if the next character is not a dot or colon, ([.:]?) matches nothing. Then .{0,65} matches up to 65 of anything, including . or :. I think this is what you’re looking for:

    $source='A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids.';
    $word = 'regular';
    preg_match_all('#[^.:]{0,65}\b'.preg_quote($word).'\b.{0,65}#siu', $source, $matches);
    print_r($matches);
    

    output:

    Array
    (
      [0] => Array
        (
          [0] => A regular expression (regex or regexp for short) is a special text string 
          [1] =>  You can think of regular expressions as wildcards on steroids.
        )
    
    )
    

    (See it live on Ideone)

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

Sidebar

Related Questions

I want to write a regex that matches if a string contains XYZ but
Wanna write a RegEx to validate a driving license. if it doesn't start with
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a regex that will parse out the directory and filename
How do I write a .net regex which will match a string that does
I want to write a piece of software which is essentially a regex data
I'm trying to write a regular expression that validates a date. The regex needs
Wrote the following in PowersHell as a quick iTunes demonstration: $iTunes = New-Object -ComObject
I wrote a simple batch file as a PowerShell script, and I am getting
Ok I give up, I've been trying to write a regexp in ant to

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.