kiran reddy
kiran reddy

Reputation: 21

Regular Expression Pattern for this

E.g.:

url = "http://abc.xyz.com/name=test&account=google&search=google"

At Present I am using below regular Expression to replace DNS name "^((?:http?:(?:\/\/)?)?)[^\/]*";

It is working fine but it is replacing "http://abc.xyz.com/" with other string but I want to replace only domain name only ptotocal info should be as it is

i.e. abc.xyz.com with other string not http://

Ex:

String re = "/^(?:(?!https?:\/\/|\/).)+/gm"; String str = "http://abc.xyz.com/name=test&account=google&search=google";

String subst = "www.xyz.com";

System.out.println(str.replace(re, subst));

Actual : "http://abc.xyz.com/name=test&account=google&search=google" Expected : "http://www.google.com/name=test&account=google&search=google"

o/p not working

Can anyone help me?

Upvotes: 2

Views: 118

Answers (2)

vks
vks

Reputation: 67968

^(?:(?!https?:\/\/|\/).)+

You can try this.Replace by www.xyz.com.See demo.

http://regex101.com/r/sU3fA2/49

var re = /^(?:(?!https?:\/\/|\/).)+/gm;
var str = 'abc.xyz.com/name=test&account=google&search=google"\nhttp://abc.xyz.com/name=test&account=google&search=google"\nhttps://abc.xyz.com/name=test&account=google&search=google"\ntextile.trails.com/…{crea}&secure‌​_id=[SECURE_ID]&position={heigherposition}&search_device={network}&device={device‌​}&match_type={matchtype}';
var subst = 'www.xyz.com';

var result = str.replace(re, subst);

Upvotes: 1

Toto
Toto

Reputation: 91488

Have a try with:

Search for: (https?://)?\w+(.*)
and replace with: $1www$2

Upvotes: 0

Related Questions