(Java/Regex) How can I find "http://" excepting "src="http://"

qumm :

I have problem with regex on Java, right now.

I have string like:

img border=\"0\" src=\"http://www.stackoverflow.com http://nbsp;https://&lt

and wanna make a regex that find only two "http://" excepting "src=\"http://" to replace the "http://" to something else.

String input = "border=\"0\" src=\"http://www.stackoverflow.com http://";
String regexStr = "(?!src=\"http://).*$";
Pattern pattern = Pattern.compile(regexStr);
Matcher matcher = pattern.matcher(input);
if (matcher.matches())
  System.out.println("String " + input + " has the word src=\"http:// in it ");
else
  System.out.println("String " + input + " hasn't the word src=\"http:// in it ");

I'm searching related with this, but didn't find perfect answer yet.

Any comment would be appreciated. Thanks.

Allan :

You can use the following regex:

(?<!src=")http://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

One of the numerous regex that will match URLs as defined in What is a good regular expression to match a URL?

INPUT:

border="0" src="http://www.stackoverflow.com 
123 http://abc123.com asg
123 http://uvw-avc132.be abc

MATCHES:

http://abc123.com 
http://uvw-avc132.be

DEMO: https://regex101.com/r/SOyEtd/2

If you want to get only the http://, then use only:

(?<!src=")http://

DEMO: https://regex101.com/r/SOyEtd/4

Embedded in Java:

    String input = "border=\"0\" src=\"http://www.stackoverflow.com http://";
    String output=input.replaceAll("(?<!src=\")http://","something else");
    System.out.println(output);

OUTPUT:

border="0" src="http://www.stackoverflow.com something else

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135778&siteId=1