Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.URL;
- import java.net.MalformedURLException;
- // Replaces URLs with html hrefs codes
- public class URLInString {
- public static void main(String[] args) {
- String s = args[0];
- // separete input by spaces ( URLs don't have spaces )
- String [] parts = s.split("\s");
- // Attempt to convert each item into an URL.
- for( String item : parts ) try {
- URL url = new URL(item);
- // If possible then replace with anchor...
- System.out.print("<a href="" + url + "">"+ url + "</a> " );
- } catch (MalformedURLException e) {
- // If there was an URL that was not it!...
- System.out.print( item + " " );
- }
- System.out.println();
- }
- }
- "Please go to http://stackoverflow.com and then mailto:[email protected] to download a file from ftp://user:pass@someserver/someFile.txt"
- Please go to <a href="http://stackoverflow.com">http://stackoverflow.com</a> and then <a href="mailto:[email protected]">mailto:[email protected]</a> to download a file from <a href="ftp://user:pass@someserver/someFile.txt">ftp://user:pass@someserver/someFile.txt</a>
- url.getProtocol();
- (?bhttp://[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]
- if (s.StartsWith("(") && s.EndsWith(")"))
- {
- return s.Substring(1, s.Length - 2);
- }
- String originalString = "Please go to http://www.stackoverflow.com";
- String newString = originalString.replaceAll("http://.+?(com|net|org)/{0,1}", "<a href="$0">$0</a>");
- String msg = "Please go to http://stackoverflow.com";
- String withURL = msg.replaceAll("(?:https?|ftps?)://[\w/%.-]+", "<a href='$0'>$0</a>");
- System.out.println(withURL);
Add Comment
Please, Sign In to add comment