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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:01:27+00:00 2026-06-11T11:01:27+00:00

Possible Duplicate: Regex / Preg: No match, if found I want to use preg_replace

  • 0

Possible Duplicate:
Regex / Preg: No match, if found

I want to use preg_replace to replace some string if something in the string is NOT there. That is, if the substring is there, the string won’t be matched.

For example, if the string contains .png, it won’t find/match it.

example.com/image.png

Here, it will not find it because the string contains the line/substring .png.

example.com/image

Here, it will find it, because the string does not contain the line/substring .png anywhere.


For those who still does not get me.

$result = preg_replace("#http://(.*\S)[Something here that will not match the link if it finds the .png at last]#","<a href='\\1'>\\1</a>","Here is a link that should work http://example.com/; Here is a link that should NOT work http://example.com/image.png")
  • 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-11T11:01:28+00:00Added an answer on June 11, 2026 at 11:01 am

    OK, I’m going out on a limb here.

    First, you need a regex that’ll find a URL for you. Since you apparently want to find lots of invalid URLs, too, we’ll take a regex that simply considers any string of consecutive non-space character that contains a sequence <letter>.<letter>:

    \b(?=\S*[a-z]\.[a-z])\S+(?=\s|$)
    

    Then we can check that this sequence doesn’t end in .png:

    \b(?=\S*[a-z]\.[a-z])\S+(?=\s|$)(?<!\.png)
    

    Now you can use that for a replace operation, for example

    $result = preg_replace(
        '/\b           # Start at a word boundary
        (?=            # Assert that it\'s possible to match...
         \S*           # any number of non-whitespace characters
         [a-z]\.[a-z]  # followed by an ASCII letter, a dot, a letter
        )              # End of lookahead assertion
        \S+            # Match one or more non-whitespace characters
        (?=\s|$)       # until the next whitespace or end of string
        (?<!\.png)     # unless that match ends in .png/ix', 
        '<a href="\0">\0</a>', $subject);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Regex to match URL Is there a regular expression to return a
Possible Duplicate: RegEx match open tags except XHTML self-contained tags i want to grap
Possible Duplicate: Regex exec only returning first match a1b2c3d.replace(/[0-9]/g,x) returns axbxdxd as expected. /[0-9]/g.exec(a1b2c3d)
Possible Duplicate: replace all “foo” between () Hello, I tried to use regex to
Possible Duplicate: Java Regex Replace with Capturing Group Is there any way to replace
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: Is there an alternative to string.Replace that is case-insensitive? I'm looking for
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a string
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a string
Possible Duplicate: RegEx match open tags except XHTML self-contained tags how can I replace

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.