2

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.

qumm
  • 137
  • 1
  • 13

2 Answers2

3

The regular expression you want is

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

The part in the parentheses is a "negative lookbehind". It means not preceded by src=".

Of course, when you write this in a Java String literal, it will have to be

"(?<!src=\")http://"
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
1

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
Allan
  • 12,117
  • 3
  • 27
  • 51
  • 1
    No need to escape the forward slashes - they're not special in Java regular expressions. And could you maybe explain what that first, very long regular expression is doing? It's not very obvious. – Dawood ibn Kareem Dec 26 '18 at 07:52
  • 1
    Thank you for the quick and detail response.it works well. Appreciate it ! – qumm Dec 26 '18 at 07:52