java - Only take URL Part from String -
i want url string can show url in webview
.
example strings:
exp 1- hello dilip refer url www.google.com. exp 2- hi ramesh android http://android.com
i want www.google.com
, http://android.com
how can split them out of string
assuming string below can use regex below extract www.google.com , http://android.com.
string s = "hello dilip refer url www.google.com. hi ramesh android http://android.com"; pattern pc = pattern.compile("((http://)|(www.))[a-z,a-z]+.com"); matcher matcher = pc.matcher(s); while(matcher.find()) { system.out.println("string extracted "+matcher.group()); }
output
string extracted www.google.com string extracted http://android.com
note: above not work these kind of urls http://meta.stackoverflow.com ,www.google.co.uk , b3ta.com.
edit:
string s = "hello dilip refer www.google.co.uk www.google.co.in url www.google.com. hi ramesh android http://android.com hello there meta.stackoverflow.com"; pattern pc = pattern.compile("((http://)|(www.))([a-z,a-z,0-9])+((.com)|(.co.[a-z]{2}))|([a-z,a-z,0-9].[a-z,a-z,0-9])+.com"); matcher matcher = pc.matcher(s); while(matcher.find()) { system.out.println("string extracted "+matcher.group()); }
output:
string extracted www.google.co.uk string extracted www.google.co.in string extracted www.google.com string extracted http://android.com string extracted meta.stackoverflow.com
even above not perfect. if can modify above regex should you
Comments
Post a Comment