-2

I want to exit a string using regex and I need some help. I have a long string and I am interested in taking all the URLs from that string and adding a specific string to them. For example:

"You can go to www.google.com, or if you want a different engine, you can go to https://www.bing.com, whichever you prefer"

That string needs to become this:

"You can go to www.google.com/whatever, or if you want a different engine, you can go to https://www.bing.com/whatever, whichever you prefer"

Thank you in advance!

razzv
  • 85
  • 8
  • Possible duplicate of [Regular expression to find URLs within a string](http://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string) – phuclv Mar 16 '17 at 15:54

2 Answers2

1

You could accomplish this in the following way ...

  • search using [\w\.]*\w+\..*?(?=,|\s|$)\K
  • replace with /whatever

see regex demo

m87
  • 4,445
  • 3
  • 16
  • 31
1

Your question is interesting but you did not said which language to use or what you've done so far. Take a look at what I did, it's using java and it works for me.

String mydata = "You can go to www.google.com, or if you want a different engine, you can go to https://www.bing.com, whichever you prefer";
String test = mydata.replaceAll("((\\b(https?|ftp|file)://)|www.)[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", "$0/whatever");
System.out.println(test);

Basically I implemented a regex for getting matching urls. and appended whatever to matches.

Maximilien Belinga
  • 3,076
  • 2
  • 25
  • 39