URL replacement in java - java

I am trying to replace url with another url.
Below is the example of source url
http://sysserver01.internal.com/web/www/internal/projectwork/resources/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries/
so this url should be replace with below url,
http://sysserver01.internal.com/var/www/html/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries/
It means if source url comes then the part after resources in source url must be appended with /var/www/html/(and rest of part after resources in source url).
This needs to be happen with rendom set of source url that contains resources string.
I dont have enough knowldege of string manipulation. So please someone help me to solve this query. Please try to solve it in JAVA as I choose this platform for my work.

String originalUrl = "http://sysserver01.internal.com/web/www/internal/projectwork/resources/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries";
String newUrl = originalUrl.replaceAll("web/www/internal/projectwork/resources", "var/www/html");

String originalUrl = "http://sysserver01.internal.com/web/www/internal/projectwork/resources/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries";
String newUrl = originalUrl.replace("web/www/internal/projectwork/resources", "var/www/html");

Related

Regular expression for retrieving http:// from the given URL

I want to retrieve http:// or https:// as per the protocol being used from the given URL.
How can i do it using Pattern and a Matcher?
If there's some other way to do it then suggest me the snippet.
here is my URL
https://www.google.co.in/#q=retrieving+http:%2F%2F+from+url+using+java+regular+expression+
thanks in advance
Another way:
String urlString = "https://www.google.co.in";
URL url = new URL(urlString);
String protocol = url.getProtocol();
Simply something like
^(https?://)
should match

Why do i get java.net.MalformedURLException: no protocol?

I am trying to build a java program that downloads files, but i get and exemption every time.
java.net.MalformedURLException: no protocol
the code for the URL is
URL site;
String urlString = "http://www.cs.drexel.edu/~spiros/teaching/CS575/slides/java.pdf‎";
site = new URL("urlString");
I have also tried:
String urlString = "www.cs.drexel.edu/~spiros/teaching/CS575/slides/java.pdf‎";
i have tried printing urlString to the console, it is being set correctly to ether one accordingly in each test. What am i missing
This is wrong :
site = new URL("urlString");
Use the variable:
site = new URL(urlString);
"urlString" is a string literal for the literal value urlString.
That isn't a valid URL.
You probably want to reference the variable, not write a string literal.

Extract a randomly generated ID from URL

I need to extract a randomly generated part of an URL for a Selenium Test in Java.
When the browser opens a page, e.g.:
/edit_person.html?id=eb58cea3a3772ff656987792eb0a8c0f
then I'm able to show the url with:
String url = driver.getCurrentUrl();
but now I need to get only the randomly generated ID after the equals sign.
How do I extract the value of parameter id once I have the entire URL as a string in variable url?
URL.getQuery() will give the query portion as a String it is a simple regular expression match to isolate the part you want.
id=(.*) will get you what you want as long as it is the only thing in the query string.
This is how managed to solve the problem:
String url = driver.getCurrentUrl();
URL aURL = new URL(url);
url = aURL.getQuery();
String[] id = url.split("=");
System.out.println(id[1]);
Thanks to Jarrod Roberson!

How to retrieve text from url using regex in Android

I have a url and I am trying to extract the text before the third slash. I am quite new to the concept in Android. I believe the Pattern class is used to achieve this. My problem is how to.
Take for instance: http://name.mywebsite.com/images.... I only require everything before images. Could anyone point me in the right direction?
You can use the uri method getHost in java.This example will help you,
URI uri = new URI("http://name.mywebsite.com/images");
String host = uri.getHost();
/* It returns name.mywebsite.com/*/

Returned URL as String is not valid in JSF

I'm trying to make use of google api as text-to-speech. So, I build a String then should pass it as a URL to a component to obtain a MP3 with the spoken words.
So, this is my code:
URI uri = new URI("http://translate.google.com/translate_tts?tl=es&q="+ URLEncoder.encode((String)this.text.getValue(), "UTF-8"));
When I make uri.toString() its return a well formed URL. If I copy and paste this output in the browser works pefectly.
But if I assign this returned String to the source property of a ice:outputMedia is not working. Then inspect the HTML generated in the page and the String in src property is:
http://translate.google.com/translate_tts?tl=es&q=Bobby+need+peanuts
The & symbol has been replaced by &.
How can I avoid this to make a valid URL?
You need to decode the url on the client side using Javascript.
var decoded = decodeURI(URI)

Categories