Reputation: 146
I am writing a code to replace some expression as follows
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str=str.replaceAll("http\\S+", "URL "); //used answer given
System.out.println(str);
I am giving input
iCloud hits 20M users http://www.google.com/HTpUTBFK via @cnet
and want to output
ENGLISH URL ENGLISH @cnet
but I am getting
iCloud hits 20M users URL @cnet
.
After that I want to modify it to
ENGLISH URL @cnet
The modification changes all the parts of sentence not following a special character(like #,%,@ etc.) to ENGLISH.
Upvotes: 0
Views: 86
Reputation: 11953
try this
str=str.replaceAll("http://" + ".*([.]).*([/])\\w*" + "\\s", "URL ");
you are using .*
is your code. Its match all string with next space. I do not know why it not working. But if you use \\w
in place of .*
then your code will work.
Upvotes: 0
Reputation: 378
you may try this,
str=str.replaceAll("((www\\.[^\\s]+)|(https?://[^\\s]+))", "URL");
Upvotes: 0
Reputation: 13510
If what you want is to replace the web address, the regex can be much simpler:
http\\S+
And that's all!
It scans for http
and then all the non spaces it can find. It stops at the first space.
To be on the safe side and avoid the case of some word starting with http you could make the regex a bit longer and safer:
'http://\\S+'
Upvotes: 3
Reputation: 493
str=str.replaceAll("http://"+"[\\w|\\.|/]*"+"\\s", "URL ");
Using this should work for you, it starts to match from http:// and matches any normal character, dots and slashes until it encounters a space. Then replaces it with "URL ".
You could also start it with "https?://"
to match links starting with both http and https.
Upvotes: 0