remove all characters between slashes in a URL in java - java

I have a url for examle:
http://www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487
i have to convert it into:
http://www.abc.com/product.aspx?iid=34487
I am using a regex expression as :
String u = url.replaceAll("/.*?/","");
But it doesn't remove the text but just removes the slashes.? How should i correct it?

int x = url.indexOf('/');
int y = url.lastIndexOf('/')+1;
String u = url.substring(0, x) + url.substring(y);

String s = "http:www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487";
String s1 = s.replaceAll("(/(.)*/)","/");
output: http:www.abc.com/product.aspx?iid=34487

Updated to changed question
Probably not simplest solution, but works
String url = "http://www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487";
String u = "http:/" + url.replaceAll("(http://)|/.*/", "/");
Result:
http://www.abc.com/product.aspx?iid=34487

Related

String Manipulation in java 1.6

String can be like below. Using java1.6
String example = "<number>;<name-value>;<name-value>";
String abc = "+17005554141;qwq=1234;ddd=ewew;otg=383";
String abc = "+17005554141;qwq=123454";
String abc = "+17005554141";
I want to remove qwq=1234 if present from String. qwq is fixed and its value can VARY like for ex 1234 or 12345 etc
expected result :
String abc = "+17005554141;ddd=ewew;otg=383";
String abc = "+17005554141"; \\removed ;qwq=123454
String abc = "+17005554141";
I tried through
abc = abc.replaceAll(";qwq=.*;", "");
but not working.
I came up with this qwq=\d*\;? and it works. It matches for 0 or more decimals after qwq=. It also has an optional parameter ; since your example seems to include that this is not always appended after the number.
I know the question is not about javascript, but here's an example where you can see the regex working:
const regex = /qwq=\d*\;?/g;
var items = ["+17005554141;qwq=123454",
"+17005554141",
"+17005554141;qwq=1234;ddd=ewew;otg=383"];
for(let i = 0; i < items.length; i++) {
console.log("Item before replace: " + items[i]);
console.log("Item after replace: " + items[i].replace(regex, "") + "\n\n");
}
You can use regex for removing that kind of string like this. Use this code,
String example = "+17005554141;qwq=1234;ddd=ewew;otg=383";
System.out.println("Before: " + example);
System.out.println("After: " + example.replaceAll("qwq=\\d+;?", ""));
This gives following output,
Before: +17005554141;qwq=1234;ddd=ewew;otg=383
After: +17005554141;ddd=ewew;otg=383
.* applies to multi-characters, not limited to digits. Use something that applies only to bunch of digits
abc.replaceAll(";qwq=\\d+", "")
^^
Any Number
please try
abc = abc.replaceAll("qwq=[0-9]*;", "");
If you don't care about too much convenience, you can achieve this by just plain simple String operations (indexOf, replace and substring). This is maybe the most legacy way to do this:
private static String replaceQWQ(String target)
{
if (target.indexOf("qwq=") != -1) {
if (target.indexOf(';', target.indexOf("qwq=")) != -1) {
String replace =
target.substring(target.indexOf("qwq="), target.indexOf(';', target.indexOf("qwq=")) + 1);
target = target.replace(replace, "");
} else {
target = target.substring(0, target.indexOf("qwq=") - 1);
}
}
return target;
}
Small test:
String abc = "+17005554141;qwq=1234;ddd=ewew;otg=383";
String def = "+17005554141;qwq=1234";
System.out.println(replaceQWQ(abc));
System.out.println(replaceQWQ(def));
outputs:
+17005554141;ddd=ewew;otg=383
+17005554141
Another one:
abc.replaceAll(";qwq=[^;]*;", ";");
You must to use groups in replaceAll method.
Here is an example:
abc.replaceAll("(.*;)(qwq=\\d*;)(.*)", "$1$3");
More about groups you can find on: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

regex to match and replace two characters between string

I have a string String a = "(3e4+2e2)sin(30)"; and i want to show it as a = "(3e4+2e2)*sin(30)";
I am not able to write a regular expression for this.
Try this replaceAll:
a = a.replaceAll("\) *(\\w+)", ")*$1");
You can go with this
String func = "sin";// or any function you want like cos.
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)]" + func, ")*siz");
System.out.println(a);
this should work
a = a.replaceAll("\\)(\\s)*([^*+/-])", ") * $2");
String input = "(3e4+2e2)sin(30)".replaceAll("(\\(.+?\\))(.+)", "$1*$2"); //(3e4+2e2)*sin(30)
Assuming the characters within the first parenthesis will always be in similar pattern, you can split this string into two at the position where you would like to insert the character and then form the final string by appending the first half of the string, new character and second half of the string.
string a = "(3e4+2e2)sin(30)";
string[] splitArray1 = Regex.Split(a, #"^\(\w+[+]\w+\)");
string[] splitArray2 = Regex.Split(a, #"\w+\([0-9]+\)$");
string updatedInput = splitArray2[0] + "*" + splitArray1[1];
Console.WriteLine("Input = {0} Output = {1}", a, updatedInput);
I did not try but the following should work
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)](\\w+)", ")*$1");
System.out.println(a);

How to Extract text from given string?

I want to extract a perticular image path string from a given string .
The String is http:\localhost:9090\SpringMVC\images\integration-icon.png
Now i want to get only the path after images like
\images\integration-icon.png
i tried this
Pattern pattern = Pattern.compile("SpringMVC");
Matcher matcher = pattern.matcher(str);
System.out.println("Checking");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
how can i get ?
String filename = filepath.substring(filepath.lastIndexOf("\\") + 1);
or (haven't tried and looks somewhat odd)
String filename = filepath.substring(filepath.lastIndexOf("\\", "images\\".length()) + 1);
String string = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
int index = string.indexOf("images\\");
String output = string.substring(index);
String text = "http:\localhost:9090\SpringMVC\images\integration-icon.png"
String subText = text.subString(text.indexOf("\images"), text.length());
System.out.println(subText);
String in = "http:\\localhost:9090\\ZenoBusinessStore\\images\\integration-icon.png";
String op = in.replace("http:\\localhost:9090\\ZenoBusinessStore", "");
System.out.println(op);
ZenoBusinessStore must be the name of your project which is constant.
Now split the string
String s = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
String ary = s.split("ZenoBusinessStore");
Now the 2nd element of the array is your image path.
System.out.println(ary[1]);
Use '\\'. It's because backslash is used in escape sequence like '\n'. With a single \ the compiler have no way to know.

Usage of the java function 'split()

I would like to split the word in java based on the delimeter'-'when it appeared last.
I am expecting the result as "sweet_memory_in" and "everbodylife#gmail.com". Do we have any inbuilt function in java.
Complete word is sweet_memory_in_everbodylife#gmail.com
String s = "sweet_memory_in_everbodylife#gmail.com";
String first = s.substring(0,s.lastIndexOf("_"));
String second = s.substring(s.lastIndexOf("_")+1 );
try this
String s = "sweet_memory_in_everbodylife#gmail.com";
String s1 = s.substring(0,s.lastIndexOf("_"));
String s2 = s.substring(s.lastIndexOf("_")+1,s.length());
Regex may help. Other way is to get the last index of _ and use substring to split it.
Try out this code :
String data = "sweet_memory_in_everbodylife#gmail.com";
int lastIndex = data.lastIndexOf("_");
String firstSplit = data.substring(0, lastIndex);
String secondSplit = data.substring(lastIndex + 1, data.length());
System.out.println(firstSplit);
System.out.println(secondSplit);
Try this my friend (Javascript Codes):
var str = 'sweet_memory_in_everbodylife#gmail.com';
var arr1 = str.substring(str.lastIndexOf("_")).split("_");
var arr2 = str.split("_"+arr1[1]);
alert(arr2[0] +" --> "+arr1[1]);

Parse string starting from the end

I want to split string around only the last _ character , example:some_string_foo_bar into two substrings some_string_foo bar.
I tried to use Pattern and StringTokenizer, but they always start from the beginning of stirng. Any idea how to do this?
Use lastIndexOf; there's no reason to do a full split.
Sure, this might be of some use. Here's an example.
String source = "hello_world_foo";
int pos = source.lastIndexOf('_');
String before = source.substring(0, pos);
String after = source.substring(pos + 1);
You can use:
String strX = "some_string_foo";
String str1 = strX.substring(0,strX.lastIndexOf("_"));
String str2 = strX.subscting(strX.lastIndexOf("_"),strX.length());
String[] arr = str.split("_");
String lastOne = arr[arr.length-1];

Categories