Find every occurence of string in a file and store it in a List

Zajcew

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.

Erik

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use find to append a string to every file in a directory

From Dev

Should intern be called explicitly on every string occurence?

From Dev

search for the last occurence of a string in a file and append to it

From Dev

find the index of element the number of occurence in string

From Dev

Search every occurence of a substring in a string C#

From Dev

How to extract every occurence of tags in an XML file

From Dev

Searching for occurence of a string in a comma delimited list

From Dev

JS string replace only replacing every other occurence

From Dev

Counting the occurence of a specific string in a csv file

From Dev

Python find last occurence in a file

From Dev

find last occurence of multiple characters in a string in Python

From Dev

Find and replace every second occurence with sed

From Dev

Python read from file every time or store values in list?

From Dev

Find the 1st occurence of elements in a list of strings, based on first character in the string

From Dev

Delete the last occurence of a string in a file in unix/linux

From Dev

Find last occurence of string in multiple files

From Dev

Print every fifth occurence

From Dev

Check text/string for occurence of predefined list elements

From Dev

Find first occurence of a number in each line of a file

From Dev

Python find last occurence in a file

From Dev

How to find first occurence of character (not from the begining of the string) in cpp ?

From Dev

Find and replace every second occurence with sed

From Dev

How to replace every x-th occurence of a string in a text with RegEx?

From Dev

Find the 1st occurence of elements in a list of strings, based on first character in the string

From Dev

Find last occurence of a string using findstr on windows command line

From Dev

Split string after every 2 words and store into list

From Dev

ksh shell script to find first occurence of _ in string and remove everything until that

From Dev

php find multiple occurence of string in string

From Dev

PHP: Find last occurence of string in array

Related Related

  1. 1

    How to use find to append a string to every file in a directory

  2. 2

    Should intern be called explicitly on every string occurence?

  3. 3

    search for the last occurence of a string in a file and append to it

  4. 4

    find the index of element the number of occurence in string

  5. 5

    Search every occurence of a substring in a string C#

  6. 6

    How to extract every occurence of tags in an XML file

  7. 7

    Searching for occurence of a string in a comma delimited list

  8. 8

    JS string replace only replacing every other occurence

  9. 9

    Counting the occurence of a specific string in a csv file

  10. 10

    Python find last occurence in a file

  11. 11

    find last occurence of multiple characters in a string in Python

  12. 12

    Find and replace every second occurence with sed

  13. 13

    Python read from file every time or store values in list?

  14. 14

    Find the 1st occurence of elements in a list of strings, based on first character in the string

  15. 15

    Delete the last occurence of a string in a file in unix/linux

  16. 16

    Find last occurence of string in multiple files

  17. 17

    Print every fifth occurence

  18. 18

    Check text/string for occurence of predefined list elements

  19. 19

    Find first occurence of a number in each line of a file

  20. 20

    Python find last occurence in a file

  21. 21

    How to find first occurence of character (not from the begining of the string) in cpp ?

  22. 22

    Find and replace every second occurence with sed

  23. 23

    How to replace every x-th occurence of a string in a text with RegEx?

  24. 24

    Find the 1st occurence of elements in a list of strings, based on first character in the string

  25. 25

    Find last occurence of a string using findstr on windows command line

  26. 26

    Split string after every 2 words and store into list

  27. 27

    ksh shell script to find first occurence of _ in string and remove everything until that

  28. 28

    php find multiple occurence of string in string

  29. 29

    PHP: Find last occurence of string in array

HotTag

Archive