thepurpleowl
thepurpleowl

Reputation: 146

java expression match

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

Answers (4)

Durgpal Singh
Durgpal Singh

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

Fahad
Fahad

Reputation: 378

you may try this,

str=str.replaceAll("((www\\.[^\\s]+)|(https?://[^\\s]+))", "URL");

Upvotes: 0

Israel Unterman
Israel Unterman

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

asiew
asiew

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

Related Questions