java regex - matching part of string stored in variable - java

I need to extract up to the directory a file is in, in the folder path. To do so, I created a simple regex. Here is an example path \\myvdi\Prod\2014\10\LTCG\LTCG_v2306_03_07_2014_1226.pfd
The regex below will find exactly what I need, but my problem is storing it into a variable. This is what I have below. It fails at the string array
String[] temp = targetFile.split("\\.*\\");
folder = temp[0];
Suggestions?
Thanks!
Edit
The exception being thrown is: java.util.regex.PatternSyntaxException: Unexpected internal error near index 4

If your path is valid within your file system, I would recommend not using regex and using a File object instead:
String path = "\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
File file = new File(path);
System.out.println(file.getParent());
Output
\\myvdi\\Prod\\2014\\10\\LTCG\\

Simply, you need:
String path = "\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
String dir = path.substring(0, path.lastIndexOf("\\") + 1);

You should use Pattern & Matchers which are much more powerful; from your description I'm not sure if you want to get the whole folder path, but if it is, here is a solution:
String s = "\\\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
Pattern p = Pattern.compile("^(\\\\+[a-zA-Z0-9_-]+\\\\+([a-zA-Z0-9_-]+\\\\+)+).+$");
Matcher m = p.matcher(s);
if(m.matches()){
System.out.println(m.group(m.groupCount()-1));
}

Related

Filter the URL path of images (img src) to obtain the file name

Using JSOUP I parse an HTML page and I've found the image path, but now I need to obtain the image file name which is a part of the url path.
For example, this is the img src:
http://cdn-6.justdogbreeds.com/images/3.gif.pagespeed.ce.MVozFWTz66.gif
The file name is 3.gif.
What shall I use to obtain the name from the URL path? Perhaps regex?
I also have other url images:
http://cdn-1.justdogbreeds.com/images/**10.gif**.pagespeed.ce.gsOmm6tF7W.gif
http://cdn-4.justdogbreeds.com/images/**6.gif**.pagespeed.ce.KbjtJ32Zwx.gif
http://cdn-2.justdogbreeds.com/images/**8.gif**.pagespeed.ce.WAWhS-Qb82.gif
http://cdn-3.justdogbreeds.com/images/**7.gif**.pagespeed.ce.UKTkscU8uT.gif
Instead of regex you can use String.lastIndexOf() with String.substring().
String imgSrc = "http://cdn-1.justdogbreeds.com/images/10.gif.pagespeed.ce.gsOmm6tF7W.gif";
String imageName = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
imageName = imageName.substring(0, imageName.indexOf(".", 3));
System.out.println(imageName); // prints out 10.gif
This finds the last occurrence of forward slash ( / ), after which the image name starts. The rest of the string is the full image name. You want only the 10.gif bit, so the rest of Line 2 finds the next period after the image name.
You can use a regex replacement to get the value you need:
String filename = imgsrc.replaceAll("http://[^/]*justdogbreeds.com/images/([^/]*?\\.gif).*", "$1");
With the regex we match the whole URL, and capture the text right after the images/ and up to (including) the first .gif. The ([^/]*?\\.gif) matches 0 or more characters other than / as few as possible, and then .gif. If you have other extensions, you may either enumerate them in an alternation group (like ([^/]*?\\.(?:gif|jpe?g|png)), or use a more generic pattern [^.]+ (1 or more characters other than .):
String filename = imgsrc.replaceAll("http://[^/]*justdogbreeds.com/images/([^/]*?\\.[^.]+).*", "$1");
See IDEONE demo
String imgsrc = "http://cdn-1.justdogbreeds.com/images/10.gif.pagespeed.ce.gsOmm6tF7W.gif";
String filename = imgsrc.replaceAll("http://[^/]*justdogbreeds.com/images/([^/]*?\\.gif).*", "$1");
System.out.println(filename);

specific part of string

How can I get a specific portion of string, let suppose I have a string
file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds
and I want to get only
C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
The string is dynamic so its not always the same I just want to remove the file word from the beginning and the last portion with .ds extension
I gave it a try as
String resourceURI = configModel.eResource().getURI().toString();
//This line gives: file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds
String sourceFilePath = resourceURI.substring(6, resourceURI.length()-17)
//This line gives C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
This resourceURI.length()-17 can create problems because the SWA_Playground.ds is not always same.
How can I just remove the last portion from this string
Thanks
You should use the File class
String sourceFile = "file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds";
Sring filePath = sourceFile.substring(6);
File f = new File(filePath);
System.out.println(f.getParent());
System.out.println(f.getName());
First you remove the file:/ prefix, then you have a windows Path and you can create a File instance.
Then you use getParent() method to get the folder path, and getName() to get the file name.
Like this:
String sourceFilePath = resourceURI.substring(
resourceURI.indexOf("/") + 1, resourceURI.lastIndexOf('/') + 1);
Basically, create a substring containing everything between the first slash and the last slash.
Output will be:
C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
You need a regex:
resourceURI.replaceAll("^file:/", "").replaceAll("[^/]*$", "")
You simply want to remove everything before the first slash and after the last one?
Then do this:
String resourceURI = configModel.eResource().getURI().toString();
//This line gives: file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds
String[] sourceFilePathArray = ressourceURI.split("/");
String sourceFilePath = "";
for (int i = 1; i < sourceFilePathArray.length - 1; i++)
sourceFilePath = sourceFilePath + sourceFilePathArray[i] + "/";
//sourceFilePath now equals C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
Make use of lastIndexof() method from String class.
String resourceURI = configModel.eResource().getURI().toString();
String sourceFilePath = resourceURI.substring(6, resourceURI.lastIndexOf('.'));

extracting filename with extension form URL

Let's say I have the following string
http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png
What would be the best way to extract b84.png from it? My program will be getting a list of image URLs and I want to extract the file name with its extension from each URL.
I would recommend using URI to create a File like this:
String url = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png";
URI uri = URI.create(url);
File f = new File(uri.getPath());
The uri.getPath() returns only the path portion of the url (i.e. removes the scheme, host, etc.) and produces this:
/photos/images/original/000/748/132/b84.png
You can then use the created File object to extract the file name from the full path:
String fileName = f.getName();
System.out.println(fileName);
Output of print statement would be:
b84.png
However if you are not at all concerned by the input format of the url(s) then the substring answers are more terse. I figured I would offer an alternative. Hope it helps.
Try,
String url = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png";
String fileName = url.subString(url.lastIndexOf("/")+1);
String[] urlArray=yourUrlString.split("/");
String fileName=urlArray[urlArray.length-1];
I think, string lastIndexOf method is the best way to extract file name
String str = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png";
String result = str.substring(str.lastIndexOf('/')+1,str.length());
The method perfect for you
public String Name(String url){
url=url.replace('\\', '/');
return url.substring(url.lastIndexOf('/')+1,url.length());
}
this method returns any filename of any directory...whitout errors because convert the "\" to "/" so never will have problems with diferent directories
You can try this-
String url = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png"
url = url.substring(url.lastIndexOf("."));
If you don't png instead of .png, just change the last line to this-
url = url.substring(url.lastIndexOf(".") + 1);

How to get FolderName and FileName from the DirectoryPath

I have DirectoryPath:
data/data/in.com.jotSmart/app_custom/folderName/FileName
which is stored as a String in ArrayList
Like
ArrayList<String> a;
a.add("data/data/in.com.jotSmart/app_custom/page01/Note01.png");
Now from this path I want to get page01 as a separate string and Note01 as a separate string and stored it into two string variables. I tried a lot, but I am not able to get the result. If anyone knows help me to solve this out.
f.getParent()
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
For example
File f = new File("/home/jigar/Desktop/1.txt");
System.out.println(f.getParent());// /home/jigar/Desktop
System.out.println(f.getName()); //1.txt
Update: (based on update in question)
if data/data/in.com.jotSmart/app_custom/page01/Note01.png is valid representation of file in your file system then
for(String fileNameStr: filesList){
File file = new File(fileNameStr);
String dir = file.getParent().substring(file.getParent().lastIndexOf(File.separator) + 1);//page01
String fileName = f.getName();
if(fileName.indexOf(".")!=-1){
fileName = fileName.substring(0,fileName.lastIndexOf("."));
}
}
For folder name: file.getParentFile().getName().
For file name: file.getName().
create a file with this path...
then use these two methods to get directory name and file name.
file.getParent(); // dir name from starting till end like data/data....../page01
file.getName(); // file name like note01.png
if you need directory name as page01, you can get a substring of path u got from getparent.
How about using the .split ?
answer = str.split(delimiter);

Get path with quotes from JFileChooser?

Is it possible to let user enters a path with quotes in JFileChooser and get this path without quotes?
For example the user puts: "c:\path\to\File" in the File name text field. but when I got the selected file I got the current directory + "c:\path\to\File". Is their a way to solve this problem?
Thanks,
You could do something like so:
String str = "\"C:\\foor\\bar\"";
System.out.println(str);
String newStr = str.replaceAll("\"?(.+?)\"?", "$1");
System.out.println(newStr);
prints:
"C:\foo\bar"
C:\foo\bar
This will remove the quotes (") which are exactly at the beginning and end of the string. The ? in the regular expression denotes that the quotes might not be there, so for instance, the following should all yield the same result:
"C:\foo\bar"
"C:\foo\bar
C:\foo\bar"
C:\foo\bar
However, to my knowledge, the " character does not make part of a valid file path, so you might get around just by doing: str.replaceAll("\"","");.
EDIT: Seeing your comment and question edit, I made this short piece of code which seems to do what you are after. That said, I will not be removing my previous answer just in case someone else might find it useful.
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Hello");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(f) == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
if (file.getName().contains("\""))
{
Pattern pattern = Pattern.compile("\"?(.*?)\"?");
Matcher m = pattern.matcher(file.getName());
if (m.matches())
{
System.out.println("Group Found: " + m.group(1));
}
}
}
This seems to do what you are after, I have pasted the following in the File text box: "C:\foo\bar.txt" and the code printed C:\foo\bar.txt, excluding the initial segment.

Categories

Resources