Re: Regular expression to read non-commented lines from a file



Hi!

Here's the code snippet that prints all lines that are not comments and do not start with whitespace.
The input is a String (more general: CharSequence) containing the entire file content. DO NOT USE FOR LARGE FILES!

/* BEGIN */
String testLine = "This\n is\na\n#test";

Pattern pattern = Pattern.compile("^[^\\s#].*$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(testLine);
while (matcher.find()) {
  String l = matcher.group();
  System.out.println(l);
}
/* END */

In your case, it might be better to read lines using the BufferedReader's "readLine" method and just test if it starts with whitespace or "#":

BufferedReader reader = new BufferedReader(new FileReader(yourFile));
try {
 String line = null;
 while ((line = reader.readLine()) != null) {
   if (line.length() == 0) continue; /* skip line in case it is empty (is this correct?) */

   char c = line.charAt(0);
   if ((c == '#') || Character.isWhitespace(c)) continue;
   System.out.println(line);
 }
} finally {
 reader.close();
}

Best regards,
 Tex

"Jonny" <www.mail@xxxxxxxxxxxx> wrote in message news:XKZKe.3247$2C5.797@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

I would like to use a regular expression in Java to read those lines
from a file which are not comments and do not start with whitespace.
Commented lines start with #

Currently with grep, I am using the command:

grep -E "^[^#\ \t]" myfile

to get the lines I want, but I am having problems converting this
regular expression for use in Java.  I don't get any lines returned.

If I replace the above regular expression with ".*" in my Java code,
then all lines of myfile are returned, as you might expect, so it would
appear that the problem is only with the regular expression shown in the
above grep example.

Please can you help.

Thanks,
Jonny

.