Here is a string:
"http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"
This string is a concatenation of two URLs. I would like to extract only the second URL:
"http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"
How can I do that using Java?
Remove everything up to "http://" not found at the start:
String url2 = str.replaceAll("(?i).+(?=https?://)", "");
This will work case insensitively and match http or https protocols.
Try this. "str" is the url string
System.out.println(str.substring(str.lastIndexOf("http:")));
If you want to extract the URL, just find the last instance of http, and take the substring:
String secondUrl = firstUrl.substring(firstUrl.lastIndexOf("http"));
Try using string's .split() method, like this:
String oneURL = twoURLs.split("(?<!^)(?=http://)")[1];
This splits the string in places that are not at the end of the string, but are followed by http://. With that, you should end up with an array like this:
["http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/", "http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"]
[1] takes only the second element of that array.
Explanation and demonstration of the regex here: http://regex101.com/r/eW6mZ0
Related
I would like to extract a substring starting from particular substring.
I'm getting an array of URIs of multiple images from Photo Library via this solution. But the URIs are something like this
content://com.android.providers.media.documents/document/image%3A38
I would like to remove content:// and get only
com.android.providers.media.documents/document/image%3A38
I've searched through the Internet but found no best solution. Perhaps to avoid regex because it's kinda heavy.
At the moment I choose not to get the substring by checking after second '/' because it feels kinda "hardcoded".
Not sure if I've missed a good solution but please help.
If you need to get whatever string comes after a certain substring, in this case "content://", you could use the split method.
String string = "content://com.android.providers.media.documents/document/image%3A38";
String uri = string.split("content://")[1];
Or you could use the substring and indexOf methods like in the other answer, but add on the length of the substring.
String string = "content://com.android.providers.media.documents/document/image%3A38";
String sub = "content://";
String uri = string.substring(string.indexOf(sub) + sub.length());
You can just use the substring method in order to create new strings without content://, something like this :
String string = "content://com.android.providers.media.documents/document/image%3A38"
String secondString = string.substring(string.indexOf("com.android"));
I have a java string delimited by |-| like below.
Can't find |-| deliter based split any where else this is unique.
String agent = "iOS|-|iPhone|-|18.2.3|-|kuoipo-kjpopoo-kijhloii-kllkijii";
What is the correct regex to split the contents in string Array like below.
String[] dataarray;
dataarray[0]="iOS";
dataarray[1]="iPhone";
dataarray[2]="18.2.3";
dataarray[3]="kuoipo-kjpopoo-kijhloii-kllkijii";
Already tried:
agent.split("\\|-\\|");
Thanks in Advance.
Won't work
agent.split("|-|")
Do
agent.split("\\|-\\|")
I went through a couple of examples to replace a given sub-string from a given string with "" but could not achieve the result. The String is too long to post and it contains a sub-string which is as follows:-
/image/journal/article?img_id=24810&t=1475128689597
I want to replace this sub-string with "".Here the value of img_id and t can vary, so I would have to use regular expression. I tried with the following code:-
String regex="^/image/journal/article?img_id=([0-9])*&t=([0-9])*$";
content=content.replace(regex,"");
Here content is the original given string. But this code is actually not replacing anything from the content. So please help..any help would be appreciated .thanx in advance.
Use replaceAll works in nice way with regex
content=content.replaceAll("[0-9]*","");
Code
String content="/image/journal/article?img_id=24810&t=1475128689597";
content=content.replaceAll("[0-9]*","");
System.out.println(content);
Output :
/image/journal/article?img_id=&t=
Update : simple, might be little less cozy but easy one
String content="sas/image/journal/article?img_id=24810&t=1475128689597";
content=content.replaceAll("\\/image.*","");
System.out.println(content);
Output:
sas
If there is something more after t=1475128689597/?tag=343sdds and you want to retain ?tag=343sdds then use below
String content="sas/image/journal/article?img_id=24810&t=1475128689597/?tag=343sdds";
content=content.replaceAll("(\\/image.*[0-9]+[\\/])","");
System.out.println(content);
}
Output:
sas?tag=343sdds
If you're trying to replace the substring of the URL with two quotations like so:
/image/journal/article?img_id=""&t=""
Then you need to add escaped quotes \"\" inside your content assignment, edit your regex to only look for the numbers, and change it to replaceAll:
content=content.replaceAll(regex,"\"\"");
You can use Java regex Utility to replace your String with "" or (any desired String literal), based on given pattern (regex) as following:
String content = "ALPHA_/image/journal/article?img_id=24810&t=1475128689597_BRAVO";
String regex = "\\/image\\/journal\\/article\\?img_id=\\d+&t=\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
String replacement = matcher.replaceAll("PK");
System.out.println(replacement); // Will print ALPHA_PK_BRAVO
}
I have a set of URLs. Some of them have a string www as substring and some of them haven't. I need to remove prefixes in each URL.
I tried remove this prefixes using many variants of regexp:
newStr = str.replaceAll("http://|http://www.", "");
newStr = str.replaceAll("^http://|http://www.$", "");
newStr = str.replaceAll("http://|http://www.", "");
where str - is an inputted URL string, and newStr is the URL after replacement.
Each of these variants replaces only http:// prefix, but www. remains in result. How I can change my regexp to remove http:// string as well as http://www. string?
I know that I can use replaceAll() twice:
newStr = str.replaceAll("http://", "").replaceAll("www.", "");
But what should I do to remain one replaceAll() and edit only the regular expression?
newStr = str.replaceFirst("^(http://)?(www\\.)?", "");
please note that . in regex means anything so you need to escape it, or you will strip first 4 symbols from wwwiscool.com and you probably don't want that. And you probably want to replace only the first matching prefix.
You can use str.replace, for example :
String str = "http://www.google.com";
str.replace("http://","").replace("http:// www.","").replace("www.","");
For more information about str.replace
I have html code with img src tags pointing to urls. Some have mysite.com/myimage.png as src others have mysite.com/1234/12/12/myimage.png. I want to replace these urls with a cache file path. Im looking for something like this.
String website = "mysite.com"
String text = webContent.replaceAll(website+ "\\d{4}\\/\\d{2}\\/\\d{2}", String.valueOf(cacheDir));
This code however does not work when the url does not have the extra date stamp at the end. Does anyone know how i might achieve this? Thanks!
Try this one
mysite\.com/(\d{4}/\d{2}/\d{2}/)?
here ? means zero or more occurance
Note: use escape character \. for dot match because .(dot) is already used in regex
Sample code :
String[] webContents = new String[] { "mysite.com/myimage.png",
"mysite.com/1234/12/12/myimage.png" };
for (String webContent : webContents) {
String text = webContent.replaceAll("mysite\\.com/(\\d{4}/\\d{2}/\\d{2}/)?",
String.valueOf("mysite.com/abc/"));
System.out.println(text);
}
output:
mysite.com/abc/myimage.png
mysite.com/abc/myimage.png
You are missing a forward slash between the website.com and the first 4 digits.
String text = webContent.replaceAll(Pattern.quote(website) + "/\\d{4}\\/\\d{2}\\/\\d{2}", String.valueOf(cacheDir));
I'd also recommend using a literal for your website.com value (the Pattern.quote part).
Finally you are also missing the last forward slash after the last two digits so it won't be replaced, but that may be on purpose...
Try:
String text = webContent.replaceAll("(?<="+website+")(.*)(?=\\/)",
String.valueOf(cacheDir));