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.
Related
String: [img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]
Result I Want: [img border=0]images/bbcode/sets/misc/bullet_go.png[/img] without /scm/ text.
Issue: Text scm is not static, could be any other text in data.
What I want: Have a look to this string
[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]
Regex which can fetch a text between ] and images/bbcode/ so the regex will detect the \scm\ text and then can remove this \scm\ from String data and end result will look like
[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
PS: I am implementing this logic in Java.
you can reach the goal without using regex, too.
since you said that the other parts are static, try this:
String myStr = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
myStr = "[img border=0]" + myStr.substring(myStr.indexOf("images"));
System.out.println(myStr);
and the output will be:
[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
I have captured text between '] and /images..' and replace this text with "". Check the following demo:
String s = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
s = s.replaceAll("(?<=])/[^/]+/","");
System.out.println(s);
if [img border=0] dynamic, you can take all except /scm/
some demo
String input = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
Pattern p = Pattern.compile("(^.*\\])\\/.*?\\/(.*$)");
Matcher m = p.matcher(input);
if (m.find()) {
String output = m.replaceFirst("$1$2");
System.out.println(output);
}
// -> [img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
I found one more way to solve this same problem
String pattereString = "].*/images";
String maineString = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
maineString = maineString.replaceAll(pattereString, "images");
System.out.println(maineString);
Output:
[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
If i have string like 08-1_2-4_1517614 and if i need to fetch the value "1517614" out of this for string manipulation in java
Any help will be much appreciated
String test = " 08-1_2-4_1517614";
String [] tokens = test.split("_");
System.out.println(tokens[tokens.length - 1]);
Or with regex:
String test = " 08-1_2-4_1517614";
System.out.println(test.replaceFirst(".+_", ""));
Or:
System.out.println(test.substring(test.lastIndexOf("_") + 1));
Here is a regex replace option:
String input = "08-1_2-4_1517614";
String output = input.replaceAll("^.*_", "");
A more general regex replace option using a capture group:
String output = input.replaceAll(".*(?<!\\d)(\\d+)$", "$1");
Or we could take a substring:
String output = input.substring(input.lastIndexOf("_") + 1);
Is the pattern always the same?
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int- could be your friend.
"08-1_2-4_1517614".substring(9, "08-1_2-4_1517614".length()) e.g.
Best way to do it, in case you want to resuse other parts of the original String.
String num = "08-1_2-4_1517614";
String[] parts = num.split("_");
String value = parts[parts.length - 1];
System.out.println(value);
The most simple is to use regex to find all the numbers after the final underscore.
Pattern patt = Pattern.compile("[^_]+$");
Matcher matcher = patt.matcher("08-1_2-4_1517614");
if(matcher.find()) {
System.out.println(matcher.group());
}else{
System.out.println("No Match Found");
}
Or use
str.substring(str.lastIndexOf("_") + 1, str.length())
I'd go for the one-liner:
System.out.println(myString.substring(9, 16));
How can I replace this
String str = "KMMH12DE1433";
String pattern = "^[a-z]{2}([0-9]{2})[a-z]{1,2}([0-9]{4})$";
String str2 = str.replaceAll(pattern, "repl");
Log.e("Founded_words2",str2);
What I got: KMMH12DE1433
What I want: MH12DE1433
Try it like this using a proper java.util.regex.Pattern and a java.util.regex.Matcher:
String str = "KMMH12DE1433";
//Make the pattern, case-insensitive using (?i)
Pattern pattern = Pattern.compile("(?i)[a-z]{2}([0-9]{2})[a-z]{1,2}([0-9]{4})");
//Create the Matcher
Matcher m = pattern.matcher(str);
//Check if we find anything
if(m.find()) {
//Use what you found - with proper capturing groups you
//gain access to parts of your pattern as needed
System.out.println("Found this: " + m.group());
}
If you just want to remove the first two characters and if the first two characters will always be uppercase letters:
String str = "KMMH12DE1433";
String pattern = "^[A-Z]{2}";
String str2 = str.replaceAll(pattern, "");
Log.e("Output string: ", str2);
try this :
String a = "KMMH12DE1433";
String pattern = "^[A-Z]{2}";
String rs = a.replaceAll(pattern,"");
Please change like this
String ans=str.substring(0);
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);
What kind of method would I use to make this:
http://www.site.net/files/file1.zip
To
file1.zip?
String yourString = "http://www.site.net/files/file1.zip";
int index = yourString.lastIndexOf('/');
String targetString = yourString.substring(index + 1);
System.out.println(targetString);// file1.zip
String str = "http://www.site.net/files/file1.zip";
str = str.substring(str.lastIndexOf("/")+1);
You could use regex to extract the last part:
#Test
public void extractFileNameFromUrl() {
final Matcher matcher = Pattern.compile("[\\w+.]*$").matcher("http://www.site.net/files/file1.zip");
Assert.assertEquals("file1.zip", matcher.find() ? matcher.group(0) : null);
}
It'll return only "file1.zip". Included here as a test as I used it to validate the code.
Use split:
String[] arr = "http://www.site.net/files/file1.zip".split("/");
Then:
String lastPart = arr[arr.length-1];
Update: Another simpler way to get this:
File file = new File("http://www.site.net/files/file1.zip");
System.out.printf("Path: [%s]%n", file.getName()); // file1.zip