java regex get just the filename - java

need some help on pattern mathcing; I need to extract just the filename from a string like:
https://www.testsite.com/files/form/anonymous/api/library/ecb198be-1f05-4b0b-b0cd-7d878488a8c4/document/050cc508-1ea6-4b5f-a22b-b3edbdf6291f/media/x.jpg
just the x.jpg part
& also from this string:
<img alt="/JAGC/Images?action=AttachFile&do=get&target=Images/x.jpg">
& if they are the same image, then replace the target with the URL string.
I can regex out the the
any help please?

It doesn't need any regexp.
Use like this:
String code = "...";
String filename = code.substring(code.lastIndexOf("/")+1, code.length());
Edit:
And in the second case, you dont need the ending of the tag, so use code.length()-2

It's as simple as this:
String filename1 = url.replaceAll(".*/([^/]+)", "$1");
String filename2 = xml.replaceAll(".*/([^\"]+)\".*", "$1");
if (filename1.equals(filename2))
xml = xml.replaceAll("(.*/)([^\"]+)(\".*)", "$1" + url + "$3");

Try this:
str.replaceAll("^.*([a-z]+\\.[a-z]+).*$","$1");
The () group the filename to $1.

Related

Regex to find text between string pattren

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]

regular expression to replaceall substrings embedded in open curling brackets and followed by equal sign and digits

In the follwing String
String toBeFormatted= "[[LngLatAlt{longitude=-7.125924901999952, latitude=33.831783175000055, altitude=NaN},
LngLatAlt{longitude=-5.401396163999948, latitude=35.92213140900003, altitude=NaN}]]"
1- I need to replace all "LngLatAlt{longitude=" with open bracket "["
2- also need to replace all the intermediate ", latitude=33.831783175000055, altitude=NaN}" with ",33.831783175000055]"
That way my string result :
"[[[-7.125924901999952,33.831783175000055],[-5.401396163999948,35.92213140900003]]]"
try it the following reg exp :
String regexTarget = "(\\[\\[LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget, "\\[\\[\\[");
String regexTarget0 = "(, altitude=NaN\\}, LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget0, "],\\[");
String regexTarget1 = "(, latitude=)";
toBeFormatted.replaceAll(regexTarget1, " ,");
String regexTarget2 = "(, altitude=NaN\\})";
toBeFormatted.replaceAll(regexTarget2, "]");
but it seems not working.
Thank you for your help.
try something like:
String result = toBeFormatted.replaceAll("LngLatAlt\\{longitude=([^,]+), latitude=([^,]+), ([^}]+)\\}", "[$1, $2]");
System.out.println(result);

how to read string upto certain comma with java

i need to read a file upto certain comma,for example;
String s=hii,lol,wow,and,finally
need output as hii,lol,wow,and
Dont want last comma followed with characters
As my code is reading last comma string
Example:iam getting my code out put as: finally
Below is my code
please guide me
File file =new File("C:/Users/xyz.txt");
FileInputStream inputStream = new FileInputStream(file);
String filke = IOUtils.toString(inputStream);
String[] pieces = filke.split("(?=,)");
String answer = Arrays.stream(pieces).skip(pieces.length - 1).collect(Collectors.joining());
String www=answer.substring(1);
System.out.format("Answer = \"%s\"%n", www);
You don't necessarily need to use regex for this. Just get the index of the last ',' and get the substring from 0 to that index:
String answer = "hii,lol,wow,and,finally";
String www = answer.substring(0, answer.lastIndexOf(','));
System.out.println(www); // prints hii,lol,wow,and
String in Java has a method called lastIndexOf(String str). That might come in handy for you.
Say your input is String s = "hii,lol,wow,and,finally";
You can do a String operation like:
String s = "hii,lol,wow,and,finally";
s = s.substring(0, s.lastIndexOf(","));
This gives you the output: hii,lol,wow,and
If you want to use java 8 stream to do it for you maybe try filter ?
String answer = Arrays.stream(pieces).filter(p -> !Objects.equals(p, pieces[pieces.length-1])).collect(Collectors.joining());
this will print Answer = "hii,lol,wow,and"
To have stricly regex you can use the Pattern.compile and Matcher
Pattern.compile("\w+(?=,)");
Matcher matcher = pattern.matcher(filke);
while (matcher.find()) {
System.out.println(matcher.group(1) + ","); // regex not good enough, maybe someone can edit it to include , (comma)
}
Will match hii, lol, wow, and,
See the regex example here https://regex101.com/r/1iZDjg/1

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.

Regex in a line replacement

I have this line in a text file which is in the following format:
/ text
/
I need to edit the line and remove text and have a result like this:
/
/
What regex should I use to remove the text? I have a problem because one "/" is in the line below.
How about this?
public String doMagic()
{
return "/\n /";
}
If your trying to remove all characters after a "/" you can do:
String in = "/ text\n /";
String out = in.replaceAll("/.*", "/");
you can use this regexp if line "/" starts and you don't need anything after it:
String in = "/ text\n /";
String pattern = "^(/)(.+?)(\\n.*)";
System.out.println(in.replaceAll(pattern, "$1$3"));

Categories