7 Replies Latest reply: Jul 21, 2007 5:35 PM by 807605 RSS

    java regexes problem finding a String in a file

    807605
      Hi,

      My problem is that I am trying to identify one line in a configuration file using Java regexes. That line begins with "upload_target ". My config below (please note it has # comments embedded inside of the file and I want to ignore this):

      # Loading a config
      upload_target=http://testonetwothree.com

      My current code is:
      Pattern lineBeginning = Pattern.compile("^upload_target*");
      
      try {
                  while ((line = writerFileContents.readLine()) != null) {
      
                          Matcher m = lineBeginning.matcher(line);
      
                          if (m.matches()) {
      
                                // code that currently never runs to read the line
      
                          }
      The line I'm looking for is not the first line in the file; I have tried compiling the pattern with the multiline option.

      Any help you can offer would be much appreciated.

      Thanks
        • 1. Re: java regexes problem finding a String in a file
          807605
          I would do it this way:
          import java.io.FileReader;
          import java.io.IOException;
          import java.util.Scanner;
          
          public class FileRead {
               
               
               /**
                * temp.txt contents
                * 
                * abc jimmy 
                * def samantha
                * ghi tony
                * jkl rachel
                * 
                */
               public static void main(String[] args) throws IOException {
                    Scanner reader = new Scanner(new FileReader("C:\\temp.txt"));
                    String pattern = "def";
                    
                    while(reader.hasNext()) {
                         String currLine = reader.nextLine();
                         
                         if(currLine.startsWith(pattern)) {
                              System.out.println(currLine); // prints def samantha
                              break;
                         }
                    }
                    reader.close();
               }
          You can also use indexOf on a per line basis if the pattern is not at the start of the line.
          • 2. Re: java regexes problem finding a String in a file
            807605
            Ummm?
            What are you trying to accomplish with this:
            Pattern.compile("^upload_target*");
            That pattern is looking for something that DOESNT start with a "u"
            and ends in a single "anything" character.

            Also, m.matches() checks if the ENTIRE input equals the pattern.
            You should probably be using m.find() or change the pattern to this:
            Pattern.compile(".*upload_target.*);

            You might want to make use of capture groups.
            • 3. Re: java regexes problem finding a String in a file
              807605
              here's a sample.
              notice that sample group(0) is always the entire pattern match.
              import java.util.regex.*;
              
              public class RegexTester{
              
              public static void main(String[] args){
              
                   String text = "blah blah upload_target: some value ; blah blah";
                   System.out.println("Text: " + text);
              
                   String regex = ".*?(upload_target):\\s*(.*?)\\s*;";
                   System.out.println("Regex: " + regex);
              
                   Pattern pattern = Pattern.compile(regex);
                   Matcher matcher = pattern.matcher(text);
              
                   while(matcher.find()){
              
                   for(int i = 0; i <= matcher.groupCount(); i++){
                   System.out.println("Match " + (i+1) + ": '" + matcher.group(i) + "'");
                   }
              
                   }
              
                   System.out.println("Done");
              
              }
              
              }
              • 4. Re: java regexes problem finding a String in a file
                807605
                Ummm?
                What are you trying to accomplish with this:
                Pattern.compile("^upload_target*");
                That pattern is looking for something that DOESNT
                start with a "u"
                and ends in a single "anything" character.
                I disagree. It was just missing the period before the asterix.

                This works:
                    Pattern.compile("^upload_target.*");
                An example bit of code:
                import java.io.File;
                import java.io.FileNotFoundException;
                import java.util.Scanner;
                import java.util.regex.Matcher;
                import java.util.regex.Pattern;
                
                public class Fubar
                {
                    File inFile = new File(".\\petes\\brief\\inFile.txt"); 
                    private Scanner sc = null;
                    
                    public Fubar()
                    {
                        try
                        {
                            sc = new Scanner(inFile);
                        } catch (FileNotFoundException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    
                    public void scanForMatch()
                    {
                        Pattern p = Pattern.compile("^upload_target.*");
                        while (sc.hasNext()) 
                        {
                            String line = sc.nextLine();
                            System.out.println(line);
                            Matcher m = p.matcher(line);
                
                            if (m.matches()) 
                            {
                                  System.out.println("matches: " + line);
                            }
                        }
                
                    }
                    
                    public static void main(String[] args)
                    {
                        Fubar f = new Fubar();
                        f.scanForMatch();
                    }
                
                }
                • 5. Re: java regexes problem finding a String in a file
                  807605
                  I disagree.
                  Sorry, i wasnt paying attention.
                  [^u] is 'not u' whereas ^ is the beginning of the line.
                  and the * means 'any number of'' which my brain swapped with .

                  The rest of my advice holds.
                  Using find() will work.
                  • 6. Re: java regexes problem finding a String in a file
                    807605
                    The rest of my advice holds.
                    Using find() will work.
                    Yep, I don't disagree with that part.
                    • 7. Re: java regexes problem finding a String in a file
                      807605
                      im glad i have your approval, lol.