1

I set a scanner's delimiter like:

scanner.useDelimiter("(\\s*?)(#.*?\n)(\\s*?)");

The goal is to ignore comments of the form

#comment \n

Thus:

Hello#inline comment
world.

becomes:

Hello
world.

By setting the delimiter as I did, I would think:

Hello#inline comment world.

would become:

[Hello]

and:

Hello#inline comment\n world.

would become

[Hello, world.]
AaronF
  • 2,841
  • 3
  • 22
  • 32

1 Answers1

3

I may be mistaken but it looks like you may want to use something like

scanner.useDelimiter("#.*(\r?\n|\r)?");

You need to remember that not every line ends with \n (or \r or \r\n), for instance last lines can not have \n at its end. Also lime separators can be different in different operating systems.

Edit:

Based on your comments it looks like you may need to add Scanners standard delimiter (one or more whitespaces - \\s+ or \p{javaWhitespace}+ if you preffer), so try with

scanner.useDelimiter("\\s*#.*(\r?\n|\r)?|\\s+");
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • This would work well if there was always a comment between tokens, but there isn't. Usually, there's just white space. "P3\n# CREATOR: GIMP PNM Filter Version 1.1\n10 10\n255" should become [P3, 10, 10, 255]. – AaronF Sep 06 '14 at 17:52