1

I am trying to filter some information out of a .txt file.

The file should be read, and I want to put all strings matching 'AT.*.1' in an ArrayList.

Example of the txt file:

Test text AT5X00.1 WednesdayAT5.1 January 26thAT9H99.1    AT6P6.1

And then I want an ArrayList like this:

[AT5X00.1, AT5.1, AT9H99.1, AT6P6.1]

I am not very experienced in programming, so maybe it's very easy but I'm stuck. This is what I tried so far:

        JFileChooser chooser = new JFileChooser(); 
        chooser.showOpenDialog(null);                             
        File file  = chooser.getSelectedFile();
        String filename = file.getAbsolutePath();
        textfield1.setText(filename);       
        String arr = new String();
           try {
            Scanner input = new Scanner (file);
            while (input.hasNext()) {
                textarea1.setText(input.nextLine());
            //input.close();              
                }
           } catch (FileNotFoundException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
           }
           String content = textarea1.getText();
           ArrayList<String> myList = new ArrayList<String>Arrays.asList(content.split("AT")));

This doesn't work because it only uses the last line instead of the whole file, and I tried to split it with "AT" instead of with a RE, but this doesn't work because the AT is removed then. And I also have "AT" strings in the file not ending with .1, and I don't want that strings in my list. So, I should use a RE, I think.

Sorry if my grammer is not the best, but English is not my native language. I hope anyone can help me out. Thanks :)

3
  • Use String pat = "\\bAT\\S*\\.1\\b"; (\S matches a non-whitespace char). Commented Jan 26, 2017 at 19:03
  • Do you want one list per line with each such list in a wrapper list, or one big list with all hits from all lines? Commented Jan 26, 2017 at 19:05
  • I want one big list of all hits from all lines, so all strings in the file starting with "AT" and ending with ".1", with letters or digits between. Commented Jan 26, 2017 at 19:09

2 Answers 2

2

The regex you might be looking for is:

AT[\d.]+1

See a demo on regex101.com.
Note that backslashes need to be escaped in Java's regex:

AT[\\d.]+1

To allow digits and letters, change it to:

AT[\\w.]+1
Sign up to request clarification or add additional context in comments.

4 Comments

this can also pick some unwanted digits regex101.com/r/CLxFZR/1
There also can be letters between the "AT" and the ".1". (I will change that in my post). For example ATSN3.1. So, strings should start with AT and end with ".1", with only numbers of letters between. (Then my is probably still not the good one, but I'll try to find the good one on that website, thanks!)
@PavneetSingh: Define unwanted.
\\w will also match _ underscore :)
1

To match words AT[a-zA-Z\\d]+\\.1\\b

Demo

const regex = /AT[a-zA-Z\d]+\.1\b/g;
const str = `Test text AT500.1 WednesdayAT5.1 January 26thAT999.1    AT66.1 
             AT434.43431.121212.212   AT50dddsds0.1     AT500_.1`;

const result = str.match(regex);
console.log(result);

To match only string which starts with AT use \\bAT[a-zA-Z\\d]+\\.1\\b

const regex = /\bAT[a-zA-Z\d]+\.1\b/g;
const str = `Test text AT500.1 WednesdayAT5.1 January 26thAT999.1    AT66.1 
             AT434.43431.121212.212   AT50dddsds0.1     AT500_.1`;

const result = str.match(regex);
console.log(result);


you can use AT\\d+\\.1(?=$|\\s)

AT\\d+\\.1 match AT and any digit further till .1

(?=$|\\s) match till boundary or a space

Alternatively , you can use \\b instead of (?=$|\\s)

AT\\d+\\.1\\b

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.