ripher
ripher

Reputation: 103

Regular Expression to match a link but with the exception of a certain type of links

Problem: I am trying to match a link and replace it with an empty string. To add to that I do not want to match links with .png, only all other links.

So far I have come up with:

(https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*

But when I have added negative look behind for .png I have not been successful.

So basically what might be the correct regular expression to match a http link but not match http link with .png?

I would prefer a greedy approach.

Please find the expected input/output below

Expected Results

The Text Input 1:

    <img id="segmentForm:wfib" style="border: 0px;"            
    src="http://localhost:8080/example/emg/WidgetFillInTheBlankRed.com" alt=""    />
    <img src="../../img/WidgetFillInTheBlankGreen.png" alt="POB1" />

The Text Output 1 [Not Same as Input, Link Matched and Replaced with Empty String]

    <img id="segmentForm:wfib" style="border: 0px;" src="" alt="" />
    <img src="../../img/WidgetFillInTheBlankGreen.png" alt="POB1" />

The Text Input 2:

    <img id="segmentForm:wfib" style="border: 0px;"         

    src="http://localhost:8080/example/emg/WidgetFillInTheBlankRed.png" alt="" />
    <img src="../../img/WidgetFillInTheBlankGreen.png" alt="FIB1" />

The Text Output 2 [Same as Input]

    <img id="segmentForm:wfib" style="border: 0px;"     

    src="http://localhost:8080/example/emg/WidgetFillInTheBlankRed.png" alt="" />
    <img src="../../img/WidgetFillInTheBlankGreen.png" alt="FIB1" />

Upvotes: 0

Views: 91

Answers (1)

Smutje
Smutje

Reputation: 18133

(https?|ftp|gopher|telnet|file|Unsure|http):((\/\/)|(\\))+[\w\d:#@%\/;$()~_?\+-=\\\.&]*(?<!png)$

Tested using https://regex101.com/

http://server/page.com -> 1 Match
http://server/page.png -> No Match

Upvotes: 2

Related Questions