简体   繁体   English

扫描仪正则表达式分隔符问题

[英]Scanner Regex Delimiter Issue

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.]

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. 你需要记住,不是每一行都以\\n (或\\r\\r\\n )结尾,例如最后一行的末尾不能有\\n 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 根据您的评论,您可能需要添加扫描仪标准分隔符(一个或多个空格 - \\\\s+\\p{javaWhitespace}+如果您\\p{javaWhitespace}+ ),请尝试使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM