I'm working on a program, that will find every occurence of a texture name in a file and then store it's name in a List. So lets say, that i have a text file with string that looks like this:
{"sampletext":"urlDistortion":"textures/Noises/FX_GaussianNoise_10x.png","sample text sample text "url":"textures/shader/shader_test/FX_Noise_Wispy_Dense.png","urlGradient":"textures/gradients/sparks.png","blendMode": "sample text","urlMask":"textures/shader_test/FX_Radial_Grad.png"}
Every texture name starts with "textures and then goes it's location which ends with quotation mark in example "textures/gradients/sparks.png".
Now i want to extract the file name and store it in a list, so from the first occurence which is "textures/Noises/FX_GaussianNoise_10x.png" i'll get just this "FX_GaussianNoise_10x.png" part. I came with an idea, that i'll create pattern that will find "textures", skip the location and somehow copy the remaining filename part.
try {
File file;
Pattern p = Pattern.compile("textures/");
List <String> textureNames = new ArrayList<>();
for (File f : list) {
file= f.getAbsoluteFile();
Scanner scanner = new Scanner(new FileReader(file));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Matcher matcher = p.matcher(line);
while (matcher.find()){
//and here i would like to add this texture name to my list and
continue searching for the next occurence
}
System.out.println("found " +count);
}
}
}
I know that there is a start() method in a Matcher class, which returns the start index of the previous match so i could do something like this inside the while loop
String s = line.substring(matcher.start(),)
and then just add this to the list, but I don't know how could i specify the endIndex.
If anyone knows how can i do it, or if there is a better way to achieve this i'll be grateful for help.
Here is simple test case that shows an idea for your inner loop:
@Test
public void testParse() {
String line = "{\"sampletext\":\"urlDistortion\":\"textures/Noises/FX_GaussianNoise_10x.png\",\"sample text sample text \"url\":\"textures/shader/shader_test/FX_Noise_Wispy_Dense.png\",\"urlGradient\":\"textures/gradients/sparks.png\",\"blendMode\": \"sample text\",\"urlMask\":\"textures/shader_test/FX_Radial_Grad.png\"}";
Pattern p = Pattern.compile("\"(textures/[^\\\"]*)\"");
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println("found " + m.group(1));
}
}
It produces this output:
found textures/Noises/FX_GaussianNoise_10x.png
found textures/shader/shader_test/FX_Noise_Wispy_Dense.png
found textures/gradients/sparks.png
found textures/shader_test/FX_Radial_Grad.png
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments